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
 Transact-SQL (2000)
 Stored Procedure Like % Syntax

Author  Topic 

Eulibrius7
Starting Member

6 Posts

Posted - 2004-06-11 : 02:26:46
The following code is giving me this error when it is executed, any ideas on how to fix the syntax?:


Incorrect syntax near the keyword 'WHERE'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'WHERE'.



CREATE Procedure search_db
@TableName VarChar(100),
@Column VarChar(100),
@Keyword VarChar(100)
AS

Declare @SQL VarChar(1000)

SELECT @SQL = 'SELECT * FROM ' + @TableName + ' WHERE ' + @Column + ' LIKE ' + '''' + '%' + @Keyword + '%' + ''''

Exec ( @SQL)
GO

timmy
Master Smack Fu Yak Hacker

1242 Posts

Posted - 2004-06-11 : 02:34:50
You can't use this syntax.

You need to use the stored procedure sp_ExecuteSQL:

Declare @SQL NVarChar(1000)

SET @SQL = N'SELECT * FROM ' + @TableName + ' WHERE ' + @Column + ' LIKE ' + '''' + '%' + @Keyword + '%' + ''''

exec sp_ExecuteSQL @SQL


Go to Top of Page
   

- Advertisement -