I’m stuck on the following sql server:
DECLARE @sql AS NVARCHAR(500),@db as varchar(50),@value AS CHAR(129);
SET @db = 'SSCT1';
SET @value = '1806-11-801-1000';
SET @sql = 'SELECT ACTINDX FROM ' + quotename(@db)
+ '.[dbo].[GL00105] WHERE ACTNUMST = ' + @value;
EXEC (@sql);
When I run this in sql server I get :
Conversion failed when converting the varchar value ‘1806-11-801-1000
I checked the field I’m using the where clause against and it matches the type in the declaration (char(129) so I’m not sure what it’s trying to convert.
I’m trying to build a sql statement that will accept the db name as a variable in addition to the value. any thoughts?
thanks
I’m going to guess that
ACTNUMSTis a string column, in which case you need to delimit it correctly:If
@valuemight ever contain apostrophes, you need to deal with that further:Yuck. A much safer approach is:
This guards a little better against dynamic SQL and also prevents you from having to deal with the quotes.