Solved

Altering a table using Dynamic Query

Posted on 2009-06-28
6
150 Views
Last Modified: 2012-05-07
I'm using a Dynamic Query to alter a temp table in Stored Procedure.  I'm using Values to name the columns from a Cursor.
My problem is that the values are numbers and will contain negivitve numbers.  ex:-55, -125 etc.
When the table is altered with the column name that has a "-",
I get a syntax error: "Incorrect syntax near '-'."
I have casted the value to a string and even added a "t" in front of the value, but still get the error.

Any help would be great.
Thanks!
...

--@rtemps = -55

BEGIN 

	DECLARE tempvalue_cursor CURSOR FOR

	SELECT R1 From dbo.LVWAREQUALIMPORT

	SET @rtemps = CAST(@rtemps AS VARCHAR(10))

	exec ('ALTER TABLE #t ADD T'+ @rtemps+' VARCHAR(10) NULL')

...

Open in new window

0
Comment
Question by:wayneburr
  • 2
  • 2
  • 2
6 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24733314
Hello wayneburr,


exec ('ALTER TABLE #t ADD  ['+ @rtemps+'] VARCHAR(10) NULL')


Regards,

Aneesh
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24733323
Use the Absolute function ABS() to get rid the of the negative in front of the number. Something like this:
 
 

...

--@rtemps = -55

BEGIN 

	DECLARE tempvalue_cursor CURSOR FOR

	SELECT R1 From dbo.LVWAREQUALIMPORT

	SET @rtemps = CAST(ABS(@rtemps) AS VARCHAR(10))

	exec ('ALTER TABLE #t ADD T'+ @rtemps+' VARCHAR(10) NULL')

...

Open in new window

0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 24733328
exec ('ALTER TABLE #t ADD  [T'+ @rtemps+'] VARCHAR(10) NULL')
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Closing Comment

by:wayneburr
ID: 31597775
A billion points to you!
I needed to keep the '-' so this works exactly what I needed.
Thanks!
0
 

Author Comment

by:wayneburr
ID: 24733392
pssandhu;

Thanks for the response!  The ABS would/did work, but I needed to keep the '-' sign.

Thanks again.
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24733411
oh okay.. No worries! Cheers!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now