JCWEBHOST
asked on
sql stored procedure
hey guys, i need to know if i can sent a sql statement to the stored procedure?
exsample: select all from login
i want to pass the statement to stored procedure, like i want to pass it like a prameter?
exsample: select all from login
i want to pass the statement to stored procedure, like i want to pass it like a prameter?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
you got the answer but basically why you wanted to do so??
you got the answer but basically why you wanted to do so??
ASKER
i am getting this output
Running [dbo].[SQL_QUERY] ( @SQL = select * from login ).
Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.
No rows affected.
(0 row(s) returned)
@RETURN_VALUE =
Finished running [dbo].[SQL_QUERY].
for this query
ALTER PROCEDURE dbo.SQL_QUERY
@SQL as varchar(max)
AS
BEGIN
SET NOCOUNT ON;
EXEC sp_executesql @SQL
END
Running [dbo].[SQL_QUERY] ( @SQL = select * from login ).
Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.
No rows affected.
(0 row(s) returned)
@RETURN_VALUE =
Finished running [dbo].[SQL_QUERY].
for this query
ALTER PROCEDURE dbo.SQL_QUERY
@SQL as varchar(max)
AS
BEGIN
SET NOCOUNT ON;
EXEC sp_executesql @SQL
END
If you notice, the param in my sample is declared as NVARCHAR, not VARCHAR.
run it like this
[dbo].[SQL_QUERY] ( @SQL = 'select * from login ').
[dbo].[SQL_QUERY] ( @SQL = 'select * from login ').
Hi,
you need to pass parameter with in single quote
Running [dbo].[SQL_QUERY] ( @SQL = 'select * from login' ).
you need to pass parameter with in single quote
Running [dbo].[SQL_QUERY] ( @SQL = 'select * from login' ).
ASKER
not working
output:
Running [dbo].[SQL_QUERY] ( @SQL = select * from login ).
Could not find stored procedure 's'.
No rows affected.
(0 row(s) returned)
@RETURN_VALUE = 0
Finished running [dbo].[SQL_QUERY].
stored procedure
ALTER PROCEDURE dbo.SQL_QUERY
@SQL as NVARCHAR
AS
BEGIN
SET NOCOUNT ON;
EXEC sp_executesql @SQL
END
input paramenter:
select * from login
output:
Running [dbo].[SQL_QUERY] ( @SQL = select * from login ).
Could not find stored procedure 's'.
No rows affected.
(0 row(s) returned)
@RETURN_VALUE = 0
Finished running [dbo].[SQL_QUERY].
stored procedure
ALTER PROCEDURE dbo.SQL_QUERY
@SQL as NVARCHAR
AS
BEGIN
SET NOCOUNT ON;
EXEC sp_executesql @SQL
END
input paramenter:
select * from login
run it with single quotes
Running [dbo].[SQL_QUERY] ( @SQL = 'select * from login' ).
Running [dbo].[SQL_QUERY] ( @SQL = 'select * from login' ).
ASKER
i did the close quotes
this is the output
Running [dbo].[SQL_QUERY] ( @SQL = 'select * from login' ).
Unclosed quotation mark after the character string ''.
No rows affected.
(0 row(s) returned)
@RETURN_VALUE = 0
Finished running [dbo].[SQL_QUERY].
this is the output
Running [dbo].[SQL_QUERY] ( @SQL = 'select * from login' ).
Unclosed quotation mark after the character string ''.
No rows affected.
(0 row(s) returned)
@RETURN_VALUE = 0
Finished running [dbo].[SQL_QUERY].
Wow, this is dragging out a while for a simple question.
// create the procedure
CREATE PROCEDURE prDoStuff
@QueryToRun NVARCHAR(2000)
AS
BEGIN
SET NOCOUNT ON;
EXEC sp_executesql @QueryToRun;
END
GO
// execute it
EXEC prDoStuff 'select * from login'
ASKER
i tried that and it gave me this output
Running [dbo].[SQL_QUERY] ( @QueryToRun = 'select * from login' ).
Incorrect syntax near 'select * from login'.
No rows affected.
(0 row(s) returned)
@RETURN_VALUE = 0
Finished running [dbo].[SQL_QUERY]
Running [dbo].[SQL_QUERY] ( @QueryToRun = 'select * from login' ).
Incorrect syntax near 'select * from login'.
No rows affected.
(0 row(s) returned)
@RETURN_VALUE = 0
Finished running [dbo].[SQL_QUERY]
It a stored procedure, not a function. Lose the brackets:
EXEC [dbo].[SQL_QUERY] @QueryToRun = 'select * from login'
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
like this?
ALTER PROCEDURE dbo.SQL_QUERY
@QueryToRun NVARCHAR(2000)
AS
EXEC @QueryToRun
ALTER PROCEDURE dbo.SQL_QUERY
@QueryToRun NVARCHAR(2000)
AS
EXEC @QueryToRun
yes , try that one
ASKER
it still give an erorr
ASKER
it work with out the quotes
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window