Hey UncleMatt..
thanks for your attempt but I tried something like that myself and it doesnt work...
thanks anyway
Main Topics
Browse All Topicsthe problem is
i want to create an insert statement with variable column-names
at first i receive a row of numbers from a select statemant for example : 2,44,565,677,899
now i wanna loop throug this numbers with a cursor and the first time i wanna insert into column '2' the second time in column '44' etc
does anyone now how to do this ???
thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
the select statement returns 44,55,66,77,88 for example
i start a loop
and in de loop i try this :
FETCH NEXT FROM MyCursor
INTO @PropertyID,@DateValue
.
insert into MyTable (pageID, @PropertyID)
values (@PageID, @DateValue )
.
as you see... the value of of @propertyID will be 44 the first loop of the cursor and 55 the second time etc
now the problem is : the first time i wanna insert into column 44 some datevalue (calculated every loop) and the next loop i wanna insert into column 55 a different datevalue..
problem is that the result of the select statement is variable.. every time you execute the select statement the result can be different.
if i run the code above i get this error
"Line 34: Incorrect syntax near '@PropertyID'."
so the insert statement doesnt accept te variable @propertyID !
i hoop hou have enough info to help me...
grtz Da Witte
it looks like you are just running an SQL statement instead of creating a statement on the fly into a string and then executing that.
if you look at this line:
exec 'Insert into table (' + @colname + ') values (' + @colvalue + ')'
it creates the SQL statement on the fly, replacing what is in @colname into the fields section of the inert statement which by the looks of it is what the problem is
Matt.
>> insert into MyTable (pageID, @PropertyID)
values (@PageID, @DateValue )
"Line 34: Incorrect syntax near '@PropertyID'."
so the insert statement doesnt accept te variable @propertyID ! <<
The actual error is in the columns list you defined. Change "insert into MyTable (pageID, @PropertyID)" to read "insert into MyTable (pageID, PropertyID)" to remove the error.
DaWitte:
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
Business Accounts
Answer for Membership
by: UncleMattPosted on 2002-12-24 at 01:57:50ID: 7628119
if column 2 is the name of the column then:
@Colname = 2
@colvalue = 44
exec 'Insert into table (' + @colname + ') values (' + @colvalue + ')'
you will need to get the data into the variables at the top or calculate each value/column on the fly using charindex perhaps
if column 2 is not the column name but the number of the column, you'll need to get the column from the syscolumns table.
i hope i've helped, but probably not as i bet it doesn't even make sense.
Matt.