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
 SQL Server Development (2000)
 Concate string in SP

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-01-07 : 08:51:11
A sanjay writes "I want to build my query by concat strings
ex:
@sql1='Select * from tblemployees where '
If @empid <> ''
@sql1=@sql1 & ' emplooyeeID=' @empid

like the above but I am unable to execute pls can anybodt show me by an example how to write an SP or suggest a good book.

"

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-01-07 : 08:54:22
???
exec(@sql1)
or sp_executeSQL -- look it up in BOL.

Go with the flow & have fun! Else fight the flow
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-01-08 : 03:59:23
Try this


@sql1='Select * from tblemployees where '
If @empid <> ''
@sql1=@sql1 & ' emplooyeeID= ' + @empid
Exec(@sql1)

Madhivanan
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2005-01-08 : 06:06:34
declare @sql varchar(4000)
If @empid <> ''
select @sql1 = coalesce(@sql1 + ' and ', '') + ' emplooyeeID = ' + @empid

select @sql = coalesce(' where ' + @sql, '')
select @sql1='Select * from tblemployees' + @sql
Exec(@sql1)


==========================================
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 -