Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can variables be declared in an Inl

Status
Not open for further replies.

AccessSQLUser

Programmer
Apr 23, 2001
101
US
Can variables be declared in an Inline Table-Valued function?
What I want to do is create a function that accepts a parameter called "DatabaseName" and then it will return a set of records from a table in that database.
CREATE FUNCTION UserStoredProcedures
(@DatabaseName sqlvariant)
RETURNS table
AS

RETURN (
SELECT name
FROM @DatabaseName..sysobjects
WHERE type = 'P' and status >= 0
)
But this doesn't work obviously but I can't put the statement in a string and then execute it either?
What do you suggest?
 
declare @xxx as varchar(...)
set @xxx = 'SELECT name from '
set @xxx = @xxx + @databasename + '..sometableWHERE type = 'P' and status >= 0'
return exec @xxx
John Fill
1c.bmp


ivfmd@mail.md
 
Thanks. I thought I tried it before but now I realized that I left out the AS keyword when declaring the variables.
It was just a silly oversight on my part.
 
Actually, it worked only when I was testing it as a SQL statement outside the Function. But when I try to create a function it doesn't work. I get an error "Incorrect syntax near the keyword 'declare'."
Here's my code:
ALTER FUNCTION UserStoredProcedures
(@DatabaseName sqlvariant)
RETURNS table
AS
declare @myString as varchar(125)

set @databaseName='AAAViperTest'
set @myString = 'SELECT name
FROM ' + @DatabaseName+'..sysobjects
WHERE type = ''P'' and status >= 0 '


RETURN (
exec (@mystring)
)
 
Keyword AS is my mistake. You shouldn't put it. John Fill
1c.bmp


ivfmd@mail.md
 
Below is my variant:

create procedure UserStoredProcedures
(@DatabaseName char(100))
AS
declare @myString varchar(125)

set @databaseName='AAAViperTest'
set @myString = 'SELECT name
FROM ' + @DatabaseName+'..sysobjects
WHERE type = ''P'' and status >= 0 '


RETURN exec @mystring
John Fill
1c.bmp


ivfmd@mail.md
 
I know that it works in a stored procedure. I was just wondering if it works in a function.
 
Change it to function. I work with SQL7 and do not know how to do it. John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top