Madhivanan is a SQL Server MVP and a very talented SQL expert. Here is one of the nuggets he shared on Just Learned.
He shared a tip where there were two interesting points to learn:
- Do not use keywords as an object name
- [Read DHall's excellent comment below]
He has given an excellent example of how GO
can be executed as a stored procedure. Here is the extension of the tip. Create a small table and now just hit EXEC GO;
and you will notice that there is a row in the table.
Create Stored Procedure:
CREATE PROCEDURE GO
AS
SELECT 1 AS NUMBER
Create Table:
CREATE TABLE T1 (ID INT)
Now execute the following code:
INSERT INTO T1(ID)
EXEC GO;
Now when selecting from table, it will give us the following result:
SELECT *
FROM T1
Now see the following resultset:
So without inserting any data, we inserted the data, well indeed good puzzle but very bad practice. Everybody should be aware of this gotcha and avoid it. Thanks Madhivanan for teaching this interesting lesson.
Republishing here with authorization of Just Learned.
Reference: Pinal Dave (http://blog.SQLAuthority.com)
Filed under: CodeProject, Pinal Dave, PostADay, SQL, SQL Authority, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, SQLServer, T SQL, Technology