Dynamic T-Sql Queries

Question posted in Computer Software on 09 2009
Rate question difficulty level 0 Votes
let's say you are to write a stored procedure that should decide upon with of its parameters
which table to select from.

for example : if the parameter value is '1', select from the customers table.
                    if the parameter value is '2', select from the orders table.

write a stored procedure that will demonstrate such a select query.
 
 
1 Answer
 

create procedure proc1
@param varchar
as

declare @sqlstr varchar(4000)
set @sqlstr = 'Select * from '

if @param 1
begin
@sqlstr = @sqlstr + 'customers'
end
else @sqlstr = @sqlstr + 'orders'

exec (@sqlstr)
GO


09/07/2009
 
 
Add an answer*
 
Email
Company: eWave LTD
Location: Glil-Yam , Israel
t-sql sql database
Now hiring!
Intuit 
---------------------------
---------------------------
---------------------------
---------------------------
---------------------------
---------------------------
---------------------------