Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Joining results from a function and a Table

Author  Topic 

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2012-01-26 : 09:36:58
The below function takes a parameter ReqNo.Iam trying to join the results returned from this parameter to a table and get an error


Error Message

Parameters were not supplied for the function 'SMD.

CREATE Function [dbo].[Maintenance] ( @ReqNo INT)
RETURNS @SM TABLE
(
Reqno numeric(9) NOT NULL,
rpt1 char(1) NULL,
recip1 varchar(50) NULL,
email1 varchar(50) NULL,
rpt2 char(1) NULL,
recip2 varchar(50) NULL,
email2 varchar(50) NULL
)
AS
BEGIN


-- The temp should be like this--
DECLARE @tmp TABLE
(
ReqNum INT identity(1,1),
rpt char(1) NULL,
ReqNO numeric(9) NULL,
recip varchar(50) NULL,
email varchar(50) NULL
)
---------------------------------

INSERT INTO @tmp (ReqNo, Rpt, recip, Email)
SELECT RequestID, Rpt, recip, Email
FROM Customer_Information
WHERE ReqNO = @ReqNO

INSERT INTO @SM(ReqNO
,rpt1
,recip1
,email1
,rpt2
,recip2
,email2)
SELECT
ReqNO = (SELECT ISNULL(ReqNO,NULL) FROM @tmp WHERE ReqNum = 1)
,Rpt1 = (SELEcT ISNULL(Rpt,'') FROM @tmp WHERE ReqNum = 1)
,Recip1 = (SELEcT ISNULL(recip,'') FROM @tmp WHERE ReqNum = 1)
,Email1 = (SELEcT ISNULL(Email,'') FROM @tmp WHERE ReqNum = 1)
,Rpt2 = (SELEcT ISNULL(Rpt,'') FROM @tmp WHERE ReqNum = 2)
,Recip2 = (SELEcT ISNULL(recip,'') FROM @tmp WHERE ReqNum = 2)
,Email2 = (SELEcT ISNULL(Email,'') FROM @tmp WHERE ReqNum = 2)
RETURN
END


Join Query

Select RR.Reqno,RR.submitteddate,RR.paymentdt,SMD.rpt1,SMD.recip1,SMD.email,
SMD.rpt2,SMD.recip2,SMD.email2
From REportInfo RR
Inner join Maintenace SMD
ON RR.reqno = SMD.reqno
where reqno = 140


How will i be able to join the function and the table or can i include the other columns from the join query in the function itself



Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-26 : 10:48:02
it should be like


Select RR.Reqno,RR.submitteddate,RR.paymentdt,SMD.rpt1,SMD.recip1,SMD.email,
SMD.rpt2,SMD.recip2,SMD.email2
From REportInfo RR
cross apply dbo.Maintenace(RR.reqno) SMD
where reqno = 140


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-26 : 10:54:36
see what all you can do with Apply operator

http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2012-01-26 : 11:39:52
Thank You so much.It worked great
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-26 : 13:17:57
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -