PDA

View Full Version : استفاده از یک متغیر بعنوان دستور sql



mehdi_moosavi
شنبه 01 اسفند 1394, 05:30 صبح
وقت دوستان بخیر
چطور قسمتی از یک دستور sql بصورت یک متغیر به دستور اضافه و کم شود؟
مثلا:

declare @str1 varchar(20=
'where a1>10'

select * from table1 @str1

hamid-nic
شنبه 01 اسفند 1394, 06:48 صبح
وقت دوستان بخیر
چطور قسمتی از یک دستور sql بصورت یک متغیر به دستور اضافه و کم شود؟
مثلا:

declare @str1 varchar(20=
'where a1>10'

select * from table1 @str1

این را امتحان کنید :
declare @str1 varchar(20)

Set @str1='where a1>10'
select * from table1 @str1

ham3d1988
یک شنبه 02 اسفند 1394, 10:56 صبح
سلام از دینامیک کوئری استفاده کنید
مثلا اینجوری:


declare @stat nvarchar(max)=''
declare @WhereClause nvarchar(max)=''
set @stat=N'select * from CustomerTbl with(nolock) where 1=1'


set @WhereClause=' and a1>1 and a2>3'
set @stat+=@WhereClause;




print @stat
exec sp_executesql @stat