How is "select *" inside a stored procedure executed?
If you have a (Sybase) stored procedure that does "select * from ... where ..." how does that work? When is the * expanded to all the columns? At sproc creation time, at sproc compilation time, execution time, etc?
I开发者_运维技巧 am using a sproc like that which is not returning one of the columns and it makes me think that it is at sproc creation time but I can't find any references to confirm that.
Edit: I have verified that recompiling a sproc doesn't cause the * to be re-evaluated which pretty much confirms my initial thoughts but still looking for a reference where this behavior is documented.
It should be execution time. You can test your theory by following these steps:
Create a dummy table MY_DUMMY_TABLE with 1 field (let's just call it "ID")
Write a stored proc that does nothing but select * from MY_DUMMY_TABLE
Observe that the stored proc does indeed return your one field
Alter the table to add another field (let's call it "NOTES")
Run your stored proc again
Observe whether "NOTES" is included in the output
The *
is expanded when the procedure is compiled, which happens at the first execution. After that, the compiled version is used.
Avoid using select *
in production code for reasons like this. See this related question.
精彩评论