• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

Dynamic SQL INSERT Error

I am trying to create some dynamic SQL, when I hard code values in it works fine but when I try and put variables in I get the error message:

Msg 207, Level 16, State 1, Line 8
Invalid column name 'JewsonTest'.

Which is refering to:

SET @BranchCSVFileName = JewsonTest (this is a table name)

Can someone please tell me where I am going wrong??

My code is as follows:

DECLARE @BranchCSVFileName varchar(255)
DECLARE @lat numeric(18,6)
DECLARE @lon numeric(18,6)
DECLARE @MaxDistance int
DECLARE @dynamicdistancesql varchar(1000)
 
SET @BranchCSVFileName = JewsonTest
SET @lat = 51.400444
SET @lon = -1.318205
SET @MaxDistance = 10
SET @dynamicdistancesql = 'INSERT INTO '+@BranchCSVFileName+' (DistanceMiles, Postcode, Lat, Lon)
SELECT * FROM (SELECT CAST(dbo.Distance('+@lat+', '+@lon+', Lat, Lon) as decimal (10,1)) As DistanceMiles, [Postcode], [Lat], [Lon]
FROM [Postcodes]
WHERE (Lat <> 0) AND (Lon <> 0)) As T WHERE DistanceMiles<='+@MaxDistance+' ORDER BY DistanceMiles'
Exec (@dynamicdistancesql)

Open in new window

0
james1977
Asked:
james1977
  • 2
1 Solution
 
TimCotteeCommented:
Hello james1977,

It still needs to be quoted as it is a string value:
Set @BranchCSVFileName = 'JewsonTest'

Regards,

TimCottee
0
 
Aneesh RetnakaranDatabase AdministratorCommented:

DECLARE @BranchCSVFileName varchar(255)
DECLARE @lat varchar(40)  ---numeric(18,6)   change 1 
DECLARE @lon varchar(40)  ---numeric(18,6)   change 2
DECLARE @MaxDistance varchar(10) --- int     change 3
DECLARE @dynamicdistancesql varchar(1000)
 
SET @BranchCSVFileName = 'JewsonTest'
SET @lat = 51.400444
SET @lon = -1.318205
SET @MaxDistance = 10
SET @dynamicdistancesql = 'INSERT INTO '+@BranchCSVFileName  +' (DistanceMiles, Postcode, Lat, Lon)
SELECT * FROM (SELECT CAST(dbo.Distance('+@lat+', '+@lon+', Lat, Lon) as decimal (10,1)) As DistanceMiles, [Postcode], [Lat], [Lon]
FROM [Postcodes]
WHERE (Lat <> 0) AND (Lon <> 0)) As T WHERE DistanceMiles<='+@MaxDistance+' ORDER BY DistanceMiles'
EXEC( @dynamicdistancesql ) 

Open in new window

0
 
james1977Author Commented:
Ah I think i've got it, once I did as you suggested it came up with an error converting varchar to numeric so I changed all my variables to varchar and it ran fine.

Thanks very much for your helping hand.
DECLARE @BranchCSVFileName varchar(255)
DECLARE @lat varchar(20)
DECLARE @lon varchar(20)
DECLARE @MaxDistance varchar(10)
DECLARE @dynamicdistancesql varchar(1000)
 
SET @BranchCSVFileName = 'JewsonTest'
SET @lat = 51.400444
SET @lon = -1.318205
SET @MaxDistance = 10
SET @dynamicdistancesql = 'INSERT INTO '+@BranchCSVFileName+' (DistanceMiles, Postcode, Lat, Lon)
SELECT * FROM (SELECT CAST(dbo.Distance('+@lat+', '+@lon+', Lat, Lon) as decimal (10,1)) As DistanceMiles, [Postcode], [Lat], [Lon]
FROM [Postcodes]
WHERE (Lat <> 0) AND (Lon <> 0)) As T WHERE DistanceMiles<='+@MaxDistance+' ORDER BY DistanceMiles'
Exec (@dynamicdistancesql)

Open in new window

0
 
james1977Author Commented:
Sorry aneeshattingal i had already sorted the problem before you posted, just got delayed replying due to some other work.

Thanks for your input guys.

James.
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now