Error with MySQL query string

Question

I’m trying to build a string to execute a dynamic query in MySQL. I built the following query based on some examples I found, but there seems to be something wrong with the syntax. Please let me know if you can see what I’m doing wrong.

DECLARE @sql varchar(32765)
SET @sql="SELECT OrgName, Phone,"
IF (Test_Service_A = 1)
BEGIN
   SET @sql = @sql + 'Test_Service_A, '
END
IF Test_Service_B = 1)
BEGIN
   SET @sql = @sql + 'Test_Service_B, '
END
...
-- DROP ', '
--@sql = SUBSTRING(@sql, 1, LEN(@sql)-2)

SET @sql = @sql + ' FROM directory_organizations WHERE Zip_Codes LIKE '%90210%''

EXEC(@sql)

0
Frank 3 months 2022-05-22T16:55:26-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse