SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


ALTER STATEMENT INSIDE PROCEDURE


ALTER STATEMENT INSIDE PROCEDURE

Author
Message
river1
river1
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8167 Visits: 1510
Hi,

If i alter a table and then try to insert a record into it i get an error....

intruction:
ALTER TABLE table1 ADD id6 int
insert into table1 values (1,1,1,1,1,1)


But if i do like this, i get no error:

ALTER TABLE table1 ADD id6 int
exec('insert into table1 values (1,1,1,1,1,1)')



Isn't there other way of doing this without using EXec commands?

Note - i can not use GO after the alter because i'm inside a stored procedure...
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (166K reputation)SSC Guru (166K reputation)SSC Guru (166K reputation)SSC Guru (166K reputation)SSC Guru (166K reputation)SSC Guru (166K reputation)SSC Guru (166K reputation)SSC Guru (166K reputation)

Group: General Forum Members
Points: 166484 Visits: 39451
Why are you adding a column to a table inside a stored procedure?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
river1
river1
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8167 Visits: 1510
That's some particular situation that we have...
kastros.george
kastros.george
SSC-Enthusiastic
SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)

Group: General Forum Members
Points: 177 Visits: 300
You cannot add a column and insert a value on the same stored procedure...
When sql server starts to execute the code it validates the script... In your case he sees tha t the alter statement is ok (without executing it) but when it gets to the insert statement it throws an error (invalid column)
The solution is to move the alter table to another stored procedure.....


PS: using exec, the code is only validated at runtime , that why you dont get aqn error
To verify the issue try this
between the alter and insert statement write a block that does nothing for 1 minute....
You 'll see that the error will rise when you hit Run (on validation) and not after 1 minute (exec)
river1
river1
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8167 Visits: 1510
Ok, still i have a question.

If i do like:


alter table .......................
exec (insert into ...............)


It works! and now i understand why, it's because the exec dont let the compiler see the insert statement.

But why does the opposite don't work?

exec(alter table .....................)
insert into ...............
kastros.george
kastros.george
SSC-Enthusiastic
SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)SSC-Enthusiastic (177 reputation)

Group: General Forum Members
Points: 177 Visits: 300
When sql server starts to execute the code it validates the script...
Validated NOT executes


so the steps are
1. validate exec statement -> ok, continue (DO NOT EXECUTE)
2. Validate insert statement -> VALIDATION ERROR no such field exists
river1
river1
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8167 Visits: 1510
thank you
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search