Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dynamic SQL INSERT Error

Posted on 2008-06-13
4
Medium Priority
?
183 Views
Last Modified: 2010-03-19
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
Comment
Question by:james1977
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 800 total points
ID: 21777509
Hello james1977,

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

Regards,

TimCottee
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 21778560

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
 

Author Comment

by:james1977
ID: 21778918
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
 

Author Comment

by:james1977
ID: 21778947
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question