Hi, I try to write a query qith some parameters based on a user record in a table.
This gives the parameters for the rest of the SP.
As there are a number of parameters I would rather change the querytext then have many queries.
Like:
I would prefer:
This is a simplified exampel.
In the real world, my queries have parameters of which several are dates:
I have no problem in having the thing running when I use straight Select-queries in my SP, but when using my 'indirect' way (via @SQL) I get conversion errors ('Syntax error converting datetime from characterstring') all the time, whatever I try.
Solutions to the problem are most welcome.
Thanks
Smitan
Code:
Select *
From tblUser
Where UserId=@Param1
As there are a number of parameters I would rather change the querytext then have many queries.
Like:
Code:
if UserField=1
Select *
From table1
else
Select *
From table2
Code:
Declare @SQL as navarchar(1000)
If UserField=1
Select @SQL='Select *
From table1'
Else
Select @SQL='Select *
From table2'
Exec @SQL
In the real world, my queries have parameters of which several are dates:
Code:
Select @SQL='Select *
From table2
Where (DateField1 Between userParam1 And
userParam2) And
DateField2 = userParam3'
Solutions to the problem are most welcome.
Thanks
Smitan