Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 How to use sp_executesql to dynamically retrieve the data

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-07-19 : 10:11:14
Karen Tan writes "Hi i am using SQL Server , i have this store procedures

declare @squery AS varchar (500)
declare @param as varchar(500)
declare @id as varchar (10)

/*This parameter is the hard code , originally it suppose to be pass from an asp page*/

set @param = 'id , name , phone '
set @id = '12'

set @squery = N' select ' + @param + N' from profile_secondary where id = ' + '''' + @id + ''''

EXECUTE sp_executesql @squery

/***
my questions is that how to i retrieve the data from the selected column i define . I would like to retrieve id , name , and phone using sp_executesql

***/"

nr
SQLTeam MVY

12543 Posts

Posted - 2003-07-19 : 17:13:18
You would have to create this sort of expression to use sp_executesql
(is id na int or char?)

declare @squery nvarchar(2000)
set @param = 'id , name , phone '
set @id = '12'
declare @id int, @name varchar(100), @phone varchar(100)

set @squery = ' select @id = id, @name = name, @phone = phone from profile_secondary where id = ' + '''' + @id + ''''

EXECUTE sp_executesql @squery, N'@id int out, @name varchar(100) out, @phone varchar(100) out', @id out, @name out, @phone out

But you could just
select @id = id, @name = name, @phone = phone from profile_secondary where id = @id

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2003-07-19 : 17:13:18
You would have to create this sort of expression to use sp_executesql
(is id na int or char?)

declare @squery nvarchar(2000)
set @param = 'id , name , phone '
set @id = '12'
declare @id int, @name varchar(100), @phone varchar(100)

set @squery = ' select @id = id, @name = name, @phone = phone from profile_secondary where id = ' + '''' + @id + ''''

EXECUTE sp_executesql @squery, N'@id int out, @name varchar(100) out, @phone varchar(100) out', @id out, @name out, @phone out

But you could just
select @id = id, @name = name, @phone = phone from profile_secondary where id = @id

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -