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.
| Author |
Topic |
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2004-11-05 : 01:48:44
|
| Please see this query of mine...declare @aidalou as varchar(800)set @aidalou = 'Select * from tblAidaLou'exec ExecuteTheQuery @aidalouIs there a way to execute a string on my storedproc?I have a purpose why i want this idea. Any help would be much appreciated.Want Philippines to become 1st World COuntry? Go for World War 3... |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2004-11-05 : 02:00:32
|
| declare @aidalou as nvarchar(800)set @aidalou = 'Select * from tblAidaLou'... in query pass the variable @aidalouexec sp_executesql @aidalouthough this is a bad idea, coz you won't get the best execution plan for this, also considering security (sql injection), erroneous sql statements passed, just to mention a few.--------------------keeping it simple... |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2004-11-05 : 02:43:10
|
| thnx jen for the response...Im creating a procedure searching for a specified field. I have 20 fields which i have to queryin a common table named tblAidaLou.exampleexec SearchFromThisField 'TheFieldName', 'StringToSearch'My code goes like this...------------------------------------------------------------if @TheFieldName = 'field1' begin select * from tblAidalou where ac = 1 and field1 = 'ffdsfsdfdsf'endif @TheFieldName = 'field2' begin select * from tblAidalou where ac = 1 and field1 = 'rgj'endand so on and so forth upto 20 fields of if. ------------------------------------------------------------Now my idea instead of having so many if conditions, ijust want to concatenate the fields by specifying it on the parameters of thestored proc and instead of so may sql statement i can put it on a singleconcatenated statement.Thus my new proc now is....------------------------------------------------------------create procedure sp_FindThis@TheField as nvarchar(100),@StringToSearch as nvarchar(500)as declare @aidalou as nvarchar(800) set @aidalou = 'select * from tblAidalou where ac = 1 and ' + @TheField + ' = ' + @StringToSearch exec sp_executesql @aidalougo------------------------------------------------------------is this a poor execution plan? Pleas scold me if necessary.Tnx...Want Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2004-11-05 : 02:54:23
|
| you can "improve" by, specifying which fields you want instead of *just pass the where clause as one statement instead of field and texttosearchbut i'm surprised the dsql vigillantes here haven't "scolded" you for using dsql...--------------------keeping it simple... |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2004-11-05 : 04:09:40
|
Please scold me here guys.....Want Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
|
|
|
|
|