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
 Development Tools
 ASP.NET
 dynamic SQL in stored procedure

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-02-03 : 08:17:12
John writes "I'm trying to write a dynamic stored procedure that takes parameters from an asp.net page. The idea is to pass the name of the table it's supposed to query, as well as a clientID, which acts as the key for the row I want to retrieve from that table. I finally have it working without generating an error ("Invalid SQL Statement. Check the server filter on the form record source"), but now it doesn't return any records...

ALTER PROCEDURE getClientData

(
@TableName varchar(255),
@clientID varchar(50)
)

AS

DECLARE @sql varchar(8000)

SET @SQL='SELECT * FROM ' + @TableName + ' WHERE clientID= '' @clientID '''

EXEC(@SQL)

RETURN


My client IDs often use email addresses with '@' inside them, but this doesn't appear to be the source of the problem...I've also seen differences in the use of apostraphes vs. quotes and have tried this both ways (errors when I use the opposite of the above). This on SQL Server2000 with Service Pack 3. Any idea what's going on here?"

nr
SQLTeam MVY

12543 Posts

Posted - 2004-02-03 : 08:26:56
for a numeric clientID
SET @SQL='SELECT * FROM ' + @TableName + ' WHERE clientID= ' + @clientID

for character
SET @SQL='SELECT * FROM ' + @TableName + ' WHERE clientID= ''' + @clientID + ''''


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