Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL doesn't like Variable names in commands?

Author  Topic 

phrankbooth
Posting Yak Master

162 Posts

Posted - 2007-01-29 : 18:15:21
Hello,
Why would this issue syntax errors? How can it be made to work?

DECLARE @TKey as BigInt
DECLARE @TName as VARCHAR(32)
DECLARE @Tid AS VARCHAR(32)

SET @TKey=99
SET @TName='TestTABLE'
SET @Tid='TestTABLEID'

SET IDENTITY_INSERT @TName ON

INSERT INTO @TName (@Tid) VALUES (@TKey)

SET IDENTITY_INSERT @TName OFF

--PhB

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-01-29 : 18:18:48
Because you need dynamic SQL to do this.

Why do you need the table name to be dynamic?

Tara Kizer
Go to Top of Page

phrankbooth
Posting Yak Master

162 Posts

Posted - 2007-01-29 : 19:39:54
Because I want this to be a generic SP for adding records to tables?

Is there a way to do this with T-SQL?

--PhB
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-01-29 : 19:44:00
Yes with dynamic SQL: http://www.sqlteam.com/item.asp?ItemID=4599

Be warned though that this is a very, very, very, very, very bad idea.

Tara Kizer
Go to Top of Page

phrankbooth
Posting Yak Master

162 Posts

Posted - 2007-01-29 : 19:59:02
You forgot one more 'very' :)
I see. I'm going to have to see what I can come up without using D-SQL.

--PhB
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-01-29 : 20:13:21
quote:
Originally posted by phrankbooth

You forgot one more 'very' :)
I see. I'm going to have to see what I can come up without using D-SQL.

--PhB



Please do share with us if you have a solution to this without using Dynamic SQL


KH

Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-01-29 : 21:51:16
quote:
Originally posted by khtan

quote:
Originally posted by phrankbooth

You forgot one more 'very' :)
I see. I'm going to have to see what I can come up without using D-SQL.

--PhB



Please do share with us if you have a solution to this without using Dynamic SQL


KH





I have a solution : normalize the database.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-01-29 : 22:12:17
quote:
Originally posted by jsmith8858
I have a solution : normalize the database.

- Jeff
http://weblogs.sqlteam.com/JeffS


+ 1

Yes. And a good solution


KH

Go to Top of Page

phrankbooth
Posting Yak Master

162 Posts

Posted - 2007-01-30 : 10:14:16
Normalization would be good if the db could be further normalized. But it's a simple db with three tables, the tables each have keys and are atomized.

I think I will just handle it by adding an SP for each table. The DB it's small enough to do that, so no big hassle, I had just wanted to try to be as generic as I could but SQL won't let me.

Thanks in any case for you help.

--PhB
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-01-30 : 10:18:07
Sometimes being too generic will cost you more than benefiting, and this is one of those cases.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page
   

- Advertisement -