469,150 Members | 1,985 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,150 developers. It's quick & easy.

dynamic column creation

hello , i have a doubt , see my requirement is to add a new column to an existing table such tat the column name should be automatically given the name of the variable . i wrote the query as
Expand|Select|Wrap|Line Numbers
  1. alter table test add'+@count+' int
, but i am getting error any idea how to solve this , waiting for your reply
Jun 6 '12 #1

✓ answered by Rabbit

You create a table with a field for the URL and all your attributes. Then you have one row per image.

10 1773
What error you got?

Please Put the Space between add and '+@count+'.
Jun 6 '12 #2
Expand|Select|Wrap|Line Numbers
  1. Incorrect syntax near '+@count+'
.
this is the error which i got .
Jun 6 '12 #3
Please send the function Coding you wrote to alter the Table.
Jun 6 '12 #4
Rabbit
12,516 Expert Mod 8TB
I have one question. Why? It sounds to me like you're breaking the rules of normalization but I can't be sure until I know the reason you need to do this. But I strongly suspect that is the case, in which case, don't do it, you're just making it more difficult for yourself down the line.
Jun 6 '12 #5
ck9663
2,878 Expert 2GB
You have to build a string for the whole statement and execute the string using EXEC. Here read some of this...

Happy Coding!!!


~~ CK
Jun 6 '12 #6
well , in my case i have to insert the url of the image inserted by the user into the db , so i wanted to create a new column every time a user inserts a new url . i think it ll make things easier for me while retrieving the value of url .
Jun 7 '12 #7
i even tried usig exec but still getting the same error , i don't think it ll make any difference in coding
Jun 7 '12 #8
Rabbit
12,516 Expert Mod 8TB
You're definitely breaking the rules of normalization. Every time the user adds a new URL, insert a new row, don't create a new column every time. You should have a URL table and just keep adding rows to that. What you're trying to do now will only make querying a nightmare later on.
Jun 7 '12 #9
well , my full requirement is like i ll add a new image and edit its attributes like width, height , once this is done , i ll add that image url as column and attributes as column values , then get its attributes by using select 'image url' from the table ... do u have any idea how to do this
Jun 8 '12 #10
Rabbit
12,516 Expert Mod 8TB
You create a table with a field for the URL and all your attributes. Then you have one row per image.
Jun 8 '12 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Tompa | last post: by
1 post views Thread by andrew queisser | last post: by
2 posts views Thread by charliewest | last post: by
reply views Thread by Mark Parter | last post: by
1 post views Thread by None | last post: by
1 post views Thread by Nagaraj | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.