Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 表值函数中的表变量_Sql_Sql Server_Sql Server 2008 R2 - Fatal编程技术网

Sql 表值函数中的表变量

Sql 表值函数中的表变量,sql,sql-server,sql-server-2008-r2,Sql,Sql Server,Sql Server 2008 R2,我在尝试启动此表值函数时出错。我试图修改它时出错。是的 “关键字“Declare”附近的语法不正确。” 然而,当我在函数之外使用它时,一切都很好。所以我只是想知道我是否遗漏了什么,或者我应该怎么做。谢谢各位 ALTER FUNCTION [dbo].[XXX]( @i_contactkey int, @v_scope varchar(15),

我在尝试启动此表值函数时出错。我试图修改它时出错。是的

“关键字“Declare”附近的语法不正确。”

然而,当我在函数之外使用它时,一切都很好。所以我只是想知道我是否遗漏了什么,或者我应该怎么做。谢谢各位

 ALTER FUNCTION [dbo].[XXX]( @i_contactkey int, 
                                                        @v_scope varchar(15), 
                                                        @i_entrykey int = null, 
                                                        @i_staffcontactkey int = null,
                                                        @d_startdate datetime,
                                                        @d_today datetime)
RETURNS TABLE
AS begin
Declare @temp as table
(contactkey int, contactkey1 int, rolekey1 int,contactkey2 int, rolekey2 int, relationshipid int) 
insert into @temp    (contactkey , contactkey1 , rolekey1 ,contactkey2 , rolekey2 , relationshipid ) 
select contactkey , contactkey1 , rolekey1 ,contactkey2 , rolekey2 , relationshipid  
from contact clcon 
        LEFT JOIN contactassociation ca on ca.contactkey2 = clcon.contactkey where  ca.rolekey1 in (4,5,6)
                    and ca.relationshipid = 181 
                    and ca.activeind = 1
                    and ca.associationkey = (select top 1 associationkey 
                                             from contactassociation 
                                             where contactkey2 = clcon.contactkey and activeind = 1
                                                and relationshipid = 181
                                                and rolekey1 in (4,5,6)
                                             order by begindate desc)



        SELECT 
             clcon.contactkey'ClientId', 
             clcon.Stat'ClientStatus', 
             ctacct.optiondesc'account',
             ctlvl.optiondesc'levelid',
            (clcon.lastname+', '+clcon.firstname)'ClientName',
             clcon.firstname'ClientFirstName', 
             clcon.lastname'ClientLastName',   
             clcon.addressline1'address1',
             clcon.addressline2'address2',
             clcon.city'city',
             dbo.getcnfgoption(81,clcon.stateid,'D')'state',
             clcon.zipcode'zipcode',
             clcon.mainphone'mainphone',
             cgcon.contactkey'cgkey',(cgcon.firstname+' '+cgcon.lastname)'CGName', 
             cgcon.firstname'CGFirstName', 
             cgcon.lastname'CGLastName', 
             cgcon.addressline1'cgaddressline1',
             cgcon.addressline2'cgaddressline2',
             cgcon.city'cgcity',
             dbo.getcnfgoption(81,cgcon.stateid,'D')'cgstate',
             cgcon.zipcode'cgzipcode',
             cgcon.mainphone'cgmainphone',  
             dbo.getClientAltCGKeys_JSON(clcon.contactkey,'J')'AltCGsJSON', 
             --dbo.getClientAltCGKeys(clcon.contactkey,'C')'AltCGNames', 
             --dbo.getClientAltCGKeys(clcon.contactkey,'L')'altcgnamekeyslast',
             --dbo.getClientAltCGKeys(clcon.contactkey,'A')'altcgkeysaddress',
             dbo.getClientEventCount(clcon.contactkey, 'M', @d_startdate, @d_today) 'MLOA',
             dbo.getClientEventCount(clcon.contactkey, 'N', @d_startdate, @d_today) 'NMLOA',
             dbo.getClientEventCount(clcon.contactkey, 'A', @d_startdate, @d_today) 'Alts',
             dbo.getClientEventCount(clcon.contactkey, 'S', @d_startdate, @d_today ) 'Suspension',
             dbo.getClientEventCountAnnual(clcon.contactkey, 'C') 'MissingNotes',           
            -- dbo.getContactVerificationStatus(clcon.contactkey, 'D')'clverification',
            -- dbo.getContactVerificationStatus(cgcon.contactkey, 'D')'cgverification',
             ed1.eventkey 'mdskey',
             dbo.getCnfgTableOption(54,ed1.eventstatusid,'D')'mdsstatus',
             ed1.ScheduledDate 'NextMDS',
             ed2.eventkey 'pockey',
             dbo.getCnfgTableOption(54,ed2.eventstatusid,'D')'pocstatus',
             ed2.ScheduledDate 'NextPoC',
             ed3.eventkey 'hvkey',
             dbo.getCnfgTableOption(54,ed3.eventstatusid,'D')'hvsstatus',
             ed3.ScheduledDate 'NextHV',
             ed4.eventkey 'medlistkey',
             dbo.getCnfgTableOption(54,ed4.eventstatusid,'D')'medstatus',
             ed4.ScheduledDate 'NextMedList',
             ed5.eventkey 'semikey',
             dbo.getCnfgTableOption(54,ed5.eventstatusid,'D')'semistatus',
             ed5.ScheduledDate 'NextSemi',          
             ed6.eventkey'placementkey',
             ed6.startdate'placementstart',
             ed6.enddate'placementend',
             [dbo].[getClientCMName](clcon.contactkey)'cmname',
             [dbo].[getClientRNName](clcon.contactkey)'rnname',
             [dbo].[getClientCMKey](clcon.contactkey)'cmkey',
             [dbo].[getClientRNKey](clcon.contactkey)'rnkey',
            alertcount=(SELECT COUNT(eventalertkey) FROM veventalert WHERE alerttype='Alert' AND clientkey=clcon.contactkey AND viewedind=0 AND contactkey=COALESCE(@i_staffcontactkey,@i_contactkey)),
            alertkey=(SELECT MAX(eventalertkey) FROM veventalert WHERE alerttype='Alert' AND clientkey=clcon.contactkey AND viewedind=0 AND contactkey=COALESCE(@i_staffcontactkey,@i_contactkey)),
            msgcount=(SELECT COUNT(eventalertkey) FROM veventalert WHERE alerttype='Message' AND clientkey=clcon.contactkey AND viewedind=0 AND cgkey=cgcon.contactkey),
            msgkey=(SELECT MAX(eventalertkey) FROM veventalert WHERE alerttype='Message' AND clientkey=clcon.contactkey AND viewedind=0 AND cgkey=cgcon.contactkey),
            clcp.birthdate
        FROM (select dbo.getcontactstatus(contactkey,'ts')'Stat',* from contact where dbo.getcontactstatus(contactkey,'ts') is not null ) clcon 
        INNER JOIN contactrole cr
            ON (clcon.contactkey = cr.contactkey)
        --Find caregiver contact info
        LEFT JOIN contactassociation ca on ca.contactkey2 = clcon.contactkey and ca.rolekey1 in (4,5,6)
                    and ca.relationshipid = 181 and ca.activeind = 1
                    and ca.associationkey = (select max(associationkey) 
                                             from contactassociation 
                                             where contactkey2 = clcon.contactkey and activeind = 1
                                                and relationshipid = 181
                                                and rolekey1 in (4,5,6))
        LEFT JOIN contact cgcon 
            ON cgcon.contactkey = ca.contactkey1 and cgcon.activeind = 1            
        LEFT JOIN contactbu cbu
            ON (clcon.contactkey = cbu.contactkey)
            /*Account/Lvl Information*/
        LEFT JOIN contactprofile clcp
            ON (clcon.contactkey=clcp.contactkey)
        LEFT JOIN cnfgtableoption  ctlvl
            ON (clcp.svclevelid = ctlvl.tableoptionkey)
        LEFT JOIN cnfgtableoption  ctacct
            ON (clcp.accountid = ctacct.tableoptionkey)

        LEFT JOIN eventdefinition ed1               /* MDS */
            ON (clcon.contactkey=ed1.contactkey AND ed1.eventkey=dbo.getContactEventByWftask(clcon.contactkey, 181, 'MINOPEN'))
        LEFT JOIN eventdefinition ed2               /* POC */
            ON (clcon.contactkey=ed2.contactkey AND ed2.eventkey=dbo.getContactEventByWftask(clcon.contactkey, 120, 'MINOPEN'))
        LEFT JOIN eventdefinition ed3               /* HV */
            ON (clcon.contactkey=ed3.contactkey AND ed3.eventkey=dbo.getContactEventByWftask(clcon.contactkey, 341, 'MINOPEN'))
        LEFT JOIN eventdefinition ed4               /* MED */
            ON (clcon.contactkey=ed4.contactkey AND ed4.eventkey=dbo.getContactEventByWftask(clcon.contactkey, 178, 'MINOPEN'))
        LEFT JOIN eventdefinition ed5               /* SEMI */
            ON (clcon.contactkey=ed5.contactkey AND ed5.eventkey=dbo.getContactEventByWftask(clcon.contactkey, 122, 'MINOPEN'))                     
        LEFT JOIN eventdefinition ed6           /* Placement */
            ON (clcon.contactkey=ed6.contactkey AND ed6.wftaskkey = 49
                    AND ed6.eventstatusid = 16
                    AND ed6.activeind = 1
                    AND ed6.enddate = (select MAX(enddate) from eventdefinition 
                                       where contactkey = clcon.contactkey and wftaskkey = 49
                                        and activeind = 1
                                        and eventstatusid = 16))

        WHERE
        --Contact info
        cr.rolekey = 8 
        AND cbu.entrykey = @i_entrykey
        --and dbo.getcontactstatus (clcon.contactkey,'TS') not in ('Closed','Discharged')
        and clcon.Stat not in ('Closed')
        and clcon.activeind=1
        -- filter by branch entrykey (if param exists)
        --order by clcon.lastname,clcon.firstname

当您在函数中使用多个语句并返回一个表时,即表值用户定义函数,您需要某种语法,例如:

CREATE FUNCTION dbo.MyFunction(@ID int)
RETURNS @Mytable TABLE 
(
    -- Columns returned by the function
    ID int PRIMARY KEY NOT NULL, 
    -- (Other columns as required)
)
AS
BEGIN

  --Various statements to populate @Mytable

  RETURN; -- Returns @Mytable
END
有关更多信息,请参阅

如果您有一个函数,它只有
返回表
,而没有返回表的定义,那么这是一个内联用户定义函数

内联用户定义函数是用户定义函数的子集 返回表数据类型的。内联函数可以用来实现 参数化视图的功能

其语法如下所示:

CREATE FUNCTION dbo.MyFunction(@ID int)
RETURNS TABLE 
AS
RETURN
(
  SELECT *
  FROM MyTable
  WHERE ID = @ID
);
这里不定义要返回的表,函数体只能是一条
SELECT
语句


目前,您的代码介于两者之间;您需要将其作为第一个选项使用,即表值用户定义函数;首先在
RETURNS
子句中定义要返回的表,然后从那里开始。

您有无效的函数声明。返回表格时,应指定保存表格的变量名,因此标题可能如下所示:

ALTER FUNCTION [dbo].[XXX](.....)
RETURNS @temp TABLE (contactkey int, contactkey1 int, rolekey1 int,contactkey2 int, rolekey2 int, relationshipid int) 
AS 
begin
...

您需要在RETURNS语句中定义表的列