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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Evaluation

Status
Not open for further replies.

PaulBarbeau

Programmer
Nov 23, 2000
109
CA
I want to build a string that i then want to use in a select statement (look at example below) and i can not workout how to do this. Can anyone help me or tell me it is imposible in t-sql


declare @sqlline varchar(100)
set @sqlline = 'op_id = 16'
select * from thetable where @sqlline
 
declare @table_name as varchar(50)
declare @sqlline varchar(100)
declare @command varchar(255)

select @table_name = 'table_name'
select @sqlline = 'where op_id = 16'
--select @sqlline = 'where description like ''%sometext%'''


set @Command = 'select * from '+@table_name+' '+@sqlline

PRINT (@Command)
EXECUTE (@Command)
 
What happend is my @command is larger then 8000 charaters
 
You can execute a string > 8000

e.g.

Code:
declare @s,@w,@o varchar(8000)
set @s = 'select ...'
set @w = ' where ....'
set @o = 'order by ...'
exec (@s + @w + @o )
 
Yeah split the query up more in to its compsite parts. You could also use aliases to reduce the number of chars

select column_name
from table_name as a
where a.column_name = 1

 
swampBoogie's theory is correct. Just you have to declare variables seperately:

Code:
DECLARE @s varchar(8000),
  @w varchar(8000),
  @o varchar(8000)

--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top