SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


table variable in a table-valued function


table variable in a table-valued function

Author
Message
Guy-591148
Guy-591148
SSC-Enthusiastic
SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)

Group: General Forum Members
Points: 149 Visits: 216
The code that is building the table variable works great outside of the code creating the function. Once I add it the the function, it would not parse.

Am I dealing with a syntax issue here? I know that I can not use a temp table in this function, which is the reason why I am going with a table variable (@T_stage).

Thanks a lot.


CREATE function new_function
(
@some_param nvarchar(20)='MISSED')

RETURNS TABLE
AS
RETURN

(BEGIN


declare @T_stage TABLE (field1 VARCHAR(40) NOT NULL,
field2 NVARCHAR (10) NOT NULL PRIMARY KEY,
field3 DATETIME NULL)



INSERT @T_COMPANY_PAYROLL_LAST


select MAX (LEGAL_NM) AS LEGAL_NAME,
MAX (TCPRL.VEND_CO_ID) AS VEND_CO_ID,
MAX(tcx.max_process_date) as LTST_PROCESSED_CHK_DATE


--INTO #T_COMPANY_PAYROLL_LAST

FROM t_bps_company_extract TCX
INNER JOIN
T_COMPANY_PAYROLL TCPRL

ON TCPRL.Vend_CO_ID=TCX.VEND_CO_ID_TEXT
WHERE TCX.Company_status = 'Active'

group by TCPRL.vend_co_id, TCX.LEGAL_NM



END

)
John Rowan
John Rowan
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21486 Visits: 4600
You don't need the table declaration section. The function itself is a table variable. Your function needs only to have the SELECT statement in it.


CREATE function new_function (@some_param nvarchar(20)='MISSED')
RETURNS TABLE
AS
RETURN(
select MAX (LEGAL_NM) AS LEGAL_NAME,
MAX (TCPRL.VEND_CO_ID) AS VEND_CO_ID,
MAX(tcx.max_process_date) as LTST_PROCESSED_CHK_DATE
FROM t_bps_company_extract TCX
INNER JOIN T_COMPANY_PAYROLL TCPRL
ON TCPRL.Vend_CO_ID=TCX.VEND_CO_ID_TEXT
WHERE TCX.Company_status = 'Active'
group by TCPRL.vend_co_id, TCX.LEGAL_NM
)
GO



John Rowan

======================================================
======================================================
Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
Guy-591148
Guy-591148
SSC-Enthusiastic
SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)

Group: General Forum Members
Points: 149 Visits: 216
John,

I appreciate your prompt reply. The reason why I am using the table declaration is that I have more code that follows the first part. I intend to join to the records in the table variable for speed reasons. Think of it almost like joining to a view in a table-valued function. If I did not have any more codes after the first set of codes, then your suggestion would be the way to go.

Thanks again.

Messan
John Rowan
John Rowan
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21486 Visits: 4600
In that case, you'll want to go with a multi-statement table valued function. Check in BOL for details, but here's a quick example.


CREATE FUNCTION dbo.udf_functest(@ID int)
RETURNS @ReturnTable TABLE (TranID int, Amount money)
AS
BEGIN
DECLARE @tempTable TABLE (ID int, TranID int, Amount money)
INSERT INTO @tempTable
SELECT ID,
TransactionID,
Amount
FROM dbo.Table1
WHERE ID = @ID

INSERT INTO @ReturnTable
SELECT TranID,
Amount
FROM dbo.Table2 t1
INNER JOIN @tempTable t2 ON t1.ID = t2.ID
RETURN
END
GO



John Rowan

======================================================
======================================================
Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
Guy-591148
Guy-591148
SSC-Enthusiastic
SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)

Group: General Forum Members
Points: 149 Visits: 216
Thank you for pointing me in the right direction.

Messan
GilaMonster
GilaMonster
SSC Guru
SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)

Group: General Forum Members
Points: 370456 Visits: 46955
Just be aware that multi-statement table valued functions tend to perform less then admirably, especially if they'll be returning lots of rows.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


atul.bajare22
atul.bajare22
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 2
In this case,
when i call new_function(), i should get value for @some_param to "missed" which you have initialized.
But i am getting "An insufficient number of arguments were supplied for the procedure or function" error.
how should i do?
daniarchitect
daniarchitect
SSC-Addicted
SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)

Group: General Forum Members
Points: 468 Visits: 333
calling object if it has a temp table or another table variable must have the same exact structure to your table valued function that you are calling. also make sure the ordinal position and datatype , size all matches.
Lowell
Lowell
SSC Guru
SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)

Group: General Forum Members
Points: 123410 Visits: 41455
function parameters are not optional the way stored procedures are. All values must be passed, or the default placehodler must be used in it's place
you have to use the default keyword if you wnat to skip the value:

not allowed:

SELECT * FROM new_function ()



the way the default value must be called instead:

SELECT * FROM new_function (default)
SELECT * FROM new_function ('MISSED')



Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
GilaMonster
GilaMonster
SSC Guru
SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)

Group: General Forum Members
Points: 370456 Visits: 46955
Please note: 6 year old thread.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search