我想知道是否有可能在mysql存储函数或存储过程中将mysql查询组成一个字符串变量,以后可以执行?我有一个get_district_part ((district_id INT,county_id INT,city_id INT,zip_id INT)) RETURNS INT引用该表的存储函数:
CREATE TABLE IF NOT EXISTS `modx`.covereage_district_part(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
districtID INT NOT NULL,
countyID INT,
cityID INT,
zipID INT,
FOREIGN KEY (districtID) REFERENCES `modx`.coverage_district (id),
FOREIGN KEY (countyID) REFERENCES `modx`.coverage_county (id),
FOREIGN KEY (cityID) REFERENCES `modx`.coverage_city (id),
FOREIGN KEY (zipID) REFERENCES `modx`.coverage_zip (id)
);
Run Code Online (Sandbox Code Playgroud)
get_distrct_part意味着返回行mathing的ID district_id及的某种组合county_id,city_id和zip_id.问题是我想返回与id的确切组合匹配的行的id,而不是包含任何这些想法的行的id.为此,我想要分割我的查询语句,以便它是专门为所提供的ID构建的.如果可以,我试图不必匹配空值.
我意识到这可以很容易地用PHP完成,但我想这样做作为mysql存储过程,如果我可以没有其他原因,那么所有其他功能都是存储过程.
我想将前 20 个行从 db_A 中的表 tbl_A 插入到 db_B 中的 tbl_B。
tbl_A 和 tbl_B 的架构是:
CREATE TABLE <tbl_name> (
id serial PRIMARY KEY,
int a,
int b
);
Run Code Online (Sandbox Code Playgroud)
我有一些与以下查询相关的问题
psql db_A
SELECT dblink_connect("dbname=db_B");
SELECT dblink_open('curse', 'SELECT id, a, b FROM tbl_B');
INSERT INTO tbl_A (SELECT id, a, b FROM dblink_fetch('curse', 20) AS (s_is int, s_a int, s_b int)) RETURNING a;
Run Code Online (Sandbox Code Playgroud)
如果有人可以评论使用游标或在存储过程中使用 dblink 或任何其他更优雅地实现上述方法的做法有多好,我将不胜感激。
database postgresql dblink prepared-statement stored-functions
我不知道为什么它在运行函数时给我错误的问题是什么
这是我的sql:
CREATE FUNCTION `test`.`GetProcessorMethodID` (processor_id INT, method_id INT)
RETURNS INTEGER
BEGIN
DECLARE id INT;
SET @id := (SELECT `processor_method_id` FROM `processor_method` WHERE `processor_id` = processor_id AND `method_id` = method_id);
RETURN @id;
END
Run Code Online (Sandbox Code Playgroud)

但是当我使用这行sql时
SELECT processor_method_id FROM test.processor_method
WHERE processor_id = 1 AND method_id = 2;
Run Code Online (Sandbox Code Playgroud)

它工作正常!它给出了我想得到的预期价值.但是在我的函数中,它并没有返回我的预期值,总是给我错误,我不知道出了什么问题
我正在尝试创建一个返回多行的函数。
以下是我的功能和类型
create or replace type emp_type
(
first_name varchar2(20)
, last_name varchar2(20)
, depart_name varchar2(20)
)
/
create or replace function get_employee
(loc in number)
return emp_type
as
emp_record emp_type;
begin
select a.first_name, a.last_name, b.department_name into emp_record.first_name,
emp_record.last_name,emp_record.depart_name
from employees a, departments b
where a.department_id=b.department_id and location_id=loc;
return(emp_record);
end;
Run Code Online (Sandbox Code Playgroud)
我用过
select get_employee(5) from dual;
Run Code Online (Sandbox Code Playgroud)
我收到“ exact fetch returns more than requested number of rows”错误。后来当我rownum<2在选择查询中使用时,我得到了“ Reference to uninitialized composite”。
能否请你帮忙?
提前致谢
我可以指定表列不是 NULL,但是如何使存储过程或函数仅与非空参数兼容?在参数名称后添加 NOT NULL 不起作用。
以下自定义存储功能 -
CREATE OR REPLACE FUNCTION words_shuffle(in_array varchar[])
RETURNS varchar[] AS
$func$
SELECT array_agg(letters.x) FROM
(SELECT UNNEST(in_array) x ORDER BY RANDOM()) letters;
$func$ LANGUAGE sql STABLE;
Run Code Online (Sandbox Code Playgroud)
在PostgreSQL 9.5.3中改组字符数组:
words=> select words_shuffle(ARRAY['a','b','c','d','e','f']);
words_shuffle
---------------
{c,d,b,a,e,f}
(1 row)
Run Code Online (Sandbox Code Playgroud)
但是现在我切换到PostgreSQL 9.6.2后,该功能停止了工作:
words=> select words_shuffle(ARRAY['a','b','c','d','e','f']);
words_shuffle
---------------
{a,b,c,d,e,f}
(1 row)
Run Code Online (Sandbox Code Playgroud)
可能是因为ORDER BY RANDOM()停止工作:
words=> select unnest(ARRAY['a','b','c','d','e','f']) order by random();
unnest
--------
a
b
c
d
e
f
(6 rows)
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个更好的方法来改组字符数组,这可以在新的PostgreSQL 9.6中使用,但也可以在9.5中使用.
我需要它用于开发中的文字游戏,它使用Pl/PgSQL函数.
更新:
汤姆莱恩回复:
现在,在ORDER BY之后扩展目标列表中的SRF.所以ORDER BY正在排序一个虚拟行,然后在此之后发生不必要的行.看到
https://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=9118d03a8
如果我创建一个函数,COMMIT在创建之后是最佳实践吗?
CREATE OR REPLACE FUNCTION myFunction (somevar varchar2)
...
END
Run Code Online (Sandbox Code Playgroud)
它执行并将函数添加到数据库中,但是我应该COMMIT在文件底部添加一个吗?也许也用BEGIN和包裹END?
好的,所以我的SQL Server 2005数据库中有以下过程.我想弄清楚为什么它没有给我任何结果.尝试访问它的用户是管理员,但我也尝试过与其他用户访问它,但我仍然没有得到任何结果.Bond在Bond表的基本查询中返回.我只想弄清楚为什么这没有结果.任何帮助表示赞赏.
ALTER PROCEDURE dbo.GetBondAmounts
(
@Username varchar(20)
)
AS
DECLARE @Admin AS bit
SELECT @Admin = Admin FROM Users WHERE Username = @Username
if(@Admin = 1)
BEGIN
RETURN
SELECT Bond.ID, SUM(Charge.BondAmount) + SUM(ChargeWithoutPower.BondAmount) Amount,
SUM(Charge.BondPremium) + SUM(ChargeWithoutPower.BondPremium) + SUM(Forfeiture.AmountPaid)
+ SUM(Forfeiture.CostOfApprehension) - SUM(Payment.Amount) - SUM(BalanceForgiveness.AmountForgiven) Balance
FROM Bond LEFT OUTER JOIN Payment ON Bond.ID = Payment.Bond
LEFT OUTER JOIN BalanceForgiveness ON Bond.ID = BalanceForgiveness.BondID
LEFT OUTER JOIN Powers ON Bond.ID = Powers.Bond
LEFT OUTER JOIN Charge ON Powers.Surety …Run Code Online (Sandbox Code Playgroud) 如何在 postgresql 中创建一个以表名作为参数的函数,该函数返回作为查询“select * from TABLE”的参数传递的表的结果集。这里的 TABLE 是传递给函数的参数。
每当我执行SHOW PROCEDURE STATUS或 时SHOW FUNCTION STATUS,引擎都会向我显示我所有数据库中的过程和函数,而不是我当前连接的数据库。
有没有一种方法可以仅从一个特定的数据库中列出和/或检索有关过程和函数的信息?例如,如果我在数据库“People”中,我希望查询仅显示该数据库中存在的函数和过程。
stored-functions ×10
mysql ×4
sql ×4
postgresql ×3
mariadb ×2
oracle ×2
plpgsql ×2
commit ×1
database ×1
dblink ×1
function ×1
oracle11g ×1
shuffle ×1
sql-server ×1