Home All Groups Group Topic Archive Search About

How to execute a combined SQL character string in stored procedure

Author
14 Jul 2006 4:11 AM
Joseph Lu
hi all,
     could any one tell me how to execute a combined SQL string in stored
procedure, thanks!

my code as follow:
/*******************************************/
CREATE procedure sp_copy3sectable

@tblname varchar(10)

as

declare @sqlstr varchar(50)

set @sqlstr = 'select * into ' + @tblname + ' from tbl_3sec'

--my question is : how to execute @sqlstr ?

GO
/*******************************************/

Author
14 Jul 2006 4:20 AM
Chris Lim
Joseph Lu wrote:
> declare @sqlstr varchar(50)
>
> set @sqlstr = 'select * into ' + @tblname + ' from tbl_3sec'
>
> --my question is : how to execute @sqlstr ?
>

exec(@sqlstr)

Chris
Author
14 Jul 2006 12:03 PM
Wayne Snyder
Joseph,
as the other poster replied you can use
EXEC (@sqlstr)

You can also use the stored procedure sp_executesql
the sp allows you do to some parameter replacement if you wish.

For your example, it really doesn't matter much either way.
--
Wayne Snyder MCDBA, SQL Server MVP
Mariner, Charlotte, NC

I support the Professional Association for SQL Server ( PASS) and it''s
community of SQL Professionals.


Show quote
"Joseph Lu" wrote:

> hi all,
>      could any one tell me how to execute a combined SQL string in stored
> procedure, thanks!
>
> my code as follow:
> /*******************************************/
> CREATE procedure sp_copy3sectable
>
> @tblname varchar(10)
>
> as
>
> declare @sqlstr varchar(50)
>
> set @sqlstr = 'select * into ' + @tblname + ' from tbl_3sec'
>
> --my question is : how to execute @sqlstr ?
>
> GO
> /*******************************************/
>
>
>

AddThis Social Bookmark Button