Conversion error when converting varchar value to int data type

I have a varchar (1000) column declared as a field containing all numbers, as shown below. And I want to execute the following script. I need this to work please

Declare @PostalCode varchar(1000)=0 set @PostalCode ='7005036,7004168,7002314,7001188,6998955' Select hl.* From CountryLocation cl INNER JOIN refPostalCodes pc ON pc.PostalCode = hl.PostalCode where pc.Postalcode in (@PostalCode) and pc.notDeleted = 1 
+4
source share
1 answer

It looks like you want to use sp_executesql :

 Declare @PostalCode varchar(1000)=0 set @PostalCode ='7005036,7004168,7002314,7001188,6998955' declare @sql nvarchar(4000) //didn't count the chars... select @sql = N'Select hl.* From CountryLocation cl INNER JOIN refPostalCodes pc ON pc.PostalCode = hl.PostalCode where pc.Postalcode in (' + @PostalCode + ') and pc.notDeleted = 1' exec sp_executesql @sql 

When coding this way, you have to be very careful about SQL injection.

+6
source

Source: https://habr.com/ru/post/1307162/


All Articles