我们可以在函数的 CASE 条件表达式中使用 SELECT 语句吗?
我尝试了以下功能来完成上述任务。
示例:我有一个用于显示包含某些行的表的函数。
create or replace function test(n integer)
returns table (name text,city text) as
$body$
begin
case n when 1 then
select * from table1
when 2 then
select * from table2
when 3 then
select * from view1
end;
end;
$body$
language plpgsql;
Run Code Online (Sandbox Code Playgroud)
--调用函数
select * from test(1);
/*have to show details of table1*/
select * from test(2);
/*have to show details of table2*/
select * from test(3);
/*have to display details of view1*/
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用“WITH”,它是 PostgreSQL 函数中的公共表表达式。
以下是示例:
例子:
Create or replace function withFunction() returns void as
$Body$
Begin
WITH cmn_l1
AS
(
SELECT "PhoneNumber1","PhoneNumber2",
DENSE_RANK() OVER(Partition by "PhoneNumber1" Order By "PhoneNumber2" )FoundIn
From tablename;
)
SELECT DISTINCT * INTO temptable
FROM cmn_l1
WHERE FoundIn > 1;
end;
$Body$
language plpgsql;
Run Code Online (Sandbox Code Playgroud)
问题:如何在函数内使用WITH执行并获取上表中的值?
我需要使用SQL Server 2008 R2从给定的日期时间获取日期名称.
示例:
给定日期:
'2014-11-14 00:00:00'
'2014-11-15 00:00:00'
Run Code Online (Sandbox Code Playgroud)
预期结果:
Date Day Name of Date
----------------------------------
2014-11-14 Friday
2014-11-15 Saturday
Run Code Online (Sandbox Code Playgroud) 我需要将值 1 分配给类型为 bit 的变量。
示例:
create or replace function test()
returns void as
$Body$
Declare
var1 bit :=0;
Begin
....
....
var1 := 1;
....
....
end;
$Body$
language plpgsql;
Run Code Online (Sandbox Code Playgroud)
错误:
ERROR: operator does not exist: bit = integer
Run Code Online (Sandbox Code Playgroud) 我试图使用PostgreSQL 9.3版本将UUID值存储到我的表中.
示例:
create table test
(
uno UUID,
name text,
address text
);
insert into test values(1,'abc','xyz');
Run Code Online (Sandbox Code Playgroud)
注意:如何将整数值存储到UUID类型中?
我正在使用PostgreSQL数据库.在这里,我需要将图像存储到数据库中,这些数据库的数量很多,例如数千个.所以我需要知道哪个数据类型使用"bytea"或"Large binary object"?
我试图将默认值设置为参数列表中的函数内的变量,但收到错误:
Run Code Online (Sandbox Code Playgroud)Create or replace function test(name varchar default null , city varchar default null , phonenumber varchar(20) default null , out sno bigint, address varchar) returns void as $$ Declare phonenumber AS VarChar(20); Begin phonenumber : =phonenumber; SELECT sno = MAX(ssno)+1 FROM emp; IF(sno IS NULL) then sno=IDENT_CURRENT('emp')+1; end; raise info '%',name; raise info '%',city; raise info '%',phonenumber; raise info '%',address; insert into emp(ename,ecity,ephonenumber,eaddress) values(name,city,phonenumber,address); end; $$ langauge plpgsql;
Create or replace function test(name varchar default null
, city …Run Code Online (Sandbox Code Playgroud) 我想使用数据透视查询的内容部分按升序对日期进行排序。
范例:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(column_date)--(column_date)
FROM #temp
order by column_date /* Error occured here*/
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
PRINT(@cols)
Run Code Online (Sandbox Code Playgroud)
通过order by子句在上述脚本附近获取错误。
错误详情:
Msg 145, Level 15, State 1, Line 4
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Run Code Online (Sandbox Code Playgroud) 当关系不存在然后发生异常时,我试图在这里显示一条错误消息。
范例:
Create or replace function fun_test() returns void as
$$
Begin
Truncate testtb;
Exception
When does_not_exist then /* When testtb does not exist*/
raise info 'Relation does not exists';
...
Run Code Online (Sandbox Code Playgroud)
错误:无法识别的异常情况“ does_not_exist”
在这里,我需要将精度转换为整数.
示例:
obj.DayDifference = !string.IsNullOrEmpty(reader["DateDiff"].ToString()) ?
(Int32)reader["DateDiff"] : 0;
Run Code Online (Sandbox Code Playgroud)
类型:
DayDifference 类型 int
DateDiff类型double precision的database table.
错误:
指定演员表无效