Solved

Problem with Declaring a Table Variable within a Table Function

Posted on 2010-08-24
3
240 Views
Last Modified: 2012-05-10
I am getting an
Incorrect syntax near the keyword 'DECLARE'
Error with them following Table Function. Anyone see anything obvious? been a while since I have needed to use Table Functions

Thanks
Nathan
CREATE FUNCTION dbo.Mist_States_for_Interval
(
--Function parameters
	@startshift datetime,
	@endshift datetime
)
RETURNS TABLE
AS
BEGIN
(
-- Create the Table Variable
DECLARE @States_For_Interval TABLE
(id integer, Equipment_id integer, Status_Id integer ,reason_id integer, location_id integer, starttime datetime, endtime datetime, Duration integer)

.....Do some stuff...

UPDATE @States_For_Interval
SET Duration = datediff(ss,@starttime,@endshift)
WHERE starttime = (SELECT MAX(endtime) FROM @States_For_Interval)


SELECT * FROM @States_For_Interval
ORDER BY starttime
	
RETURN
END
)

Open in new window

CREATE FUNCTION dbo.Mist_States_for_Interval
(
--Function parameters
	@startshift datetime,
	@endshift datetime
)
RETURNS TABLE
AS
BEGIN

-- Create the Table Variable
DECLARE @States_For_Interval TABLE
(id integer, Equipment_id integer, Status_Id integer ,reason_id integer, location_id integer, starttime datetime, endtime datetime, Duration integer)

...do some stuff..

UPDATE @States_For_Interval
SET Duration = datediff(ss,@starttime,@endshift)
WHERE starttime = (SELECT MAX(endtime) FROM @States_For_Interval)


SELECT * FROM @States_For_Interval
ORDER BY starttime
	
RETURN
END

Open in new window

0
Comment
Question by:NathanIrwin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Expert Comment

by:indianguru2
ID: 33518278
You have to define the Table structure with the Returns keyword and use that to populate and select.
0
 
LVL 11

Accepted Solution

by:
indianguru2 earned 500 total points
ID: 33518284
Attached is the SQL Server Template 
CREATE FUNCTION <Table_Function_Name> 
(
	-- Add the parameters for the function here
	<@param1, sysname, @p1> <data_type_for_param1, , int>, 
	<@param2, sysname, @p2> <data_type_for_param2, , char>
)
RETURNS 
<@Table_Variable_Name> TABLE 
(
	-- Add the column definitions for the TABLE variable here
	<Column_1, sysname, c1> <Data_Type_For_Column1, , int>, 
	<Column_2, sysname, c2> <Data_Type_For_Column2, , int>
)
AS
BEGIN
	-- Fill the table variable with the rows for your result set
	
	RETURN 
END
GO

Open in new window

0
 
LVL 7

Author Closing Comment

by:NathanIrwin
ID: 33518307
Thanks,
I tried that before, but was getting another error, I must have resolved that one because it is working now.

Cheer,
Nathan
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server Configuration Manager WMI Error 11 41
Parse this column 6 35
Finding Where Clause Value in SQL Views and SP 21 41
T-SQL: problem comparing datetime 4 50
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question