|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Friday, May 18, 2007 3:36 PM
Points: 10,040,
Visits: 1
|
|
| Comments posted to this topic are about the Question of the Day for 25 Jun 2007 posted at http://www.sqlservercentral.com/testcenter/qod.asp?QuestionID=1065.
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Wednesday, March 13, 2013 8:19 PM
Points: 712,
Visits: 95
|
|
| The script is not entirely correct. If the table creation doesn't explicitly use dbo., it creates the table under user credentials, NOT as dbo owned object. If you try to delete/truncate the table's data with dbo prefix, you will get an error Cannot find the object "emp_mgr" because it does not exist or you do not have permissions.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, January 03, 2008 3:10 PM
Points: 4,
Visits: 3
|
|
CREATE TABLE emp_mgr ( emp char(30) PRIMARY KEY, mgr char(30) NULL FOREIGN KEY REFERENCES emp_mgr(emp), NoOfReports int DEFAULT 0 ) GO INSERT emp_mgr(emp, mgr) select 'Paul', 'Alice' union all select 'Joe', 'Alice' union all select 'Alice', 'Harry' union all select 'Harry', NULL go In theory the above INSERT statement should throw a foreign constraint error because when it inserts the very first record (Paul, Alice) there is no record with 'Alice' primary key.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Friday, March 22, 2013 6:48 AM
Points: 1,291,
Visits: 138
|
|
In theory the above INSERT statement should throw a foreign constraint error... I thought so, too. I guess this is an interesting application of the UNION ALL statement. It seems that all the statements are handled in a transaction, then the foreign key constraint is checked. If you run separate INSERT statements, then the anticipated foreign constraint error does occur. This will have to be filed away in the grey matter as an interesting trick or gadget when needed for a future application.
Mark
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:03 PM
Points: 5,233,
Visits: 7,032
|
|
Hi Al, >>In theory the above INSERT statement should throw a foreign constraint error because when it inserts the very first record (Paul, Alice) there is no record with 'Alice' primary key.<< This is incorrect. There is only one INSERT statement, not a series of INSERT statements. It inserts the result of a query, that just happpens to be a bunch of UNION'ed together SELECT's for constants. However, there is no theoretic difference between this INSERT and something as INSERT INTO Tab1 (col1, col2) SELECT col3, col4 FROM Tab2 WHERE something = something_else; In other words - the whole set of rows returned by the SELECT statement is inserted at once, not row by row.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, January 03, 2008 3:10 PM
Points: 4,
Visits: 3
|
|
>In theory the above INSERT statement should throw a foreign constraint error because when it inserts the very first record (Paul, Alice) there is no record with 'Alice' primary key.
>>This is incorrect.
I've tried to run this INSERT statement in Sybase and it throws a foreign key constraint. Obviously SQL Server handles it differently and is intelligent enough to realise that given result set contains a record with 'Alice' primary key.
Would be interesting to know how Oracle treats this INSERT statement.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Monday, July 30, 2012 10:42 AM
Points: 3,434,
Visits: 519
|
|
Hi, I added ## to the table names to make tables temporary and it returned the following: 1. The message that it skips Foreign Key constraint that is not allowed on temp tables. 2. 4 rows affected. Which is expected message for the Insert statement 3. Msg 4701, Level 16, State 1, Line 1 Cannot find the object "emp_mgr" because it does not exist or you do not have permissions. This is expected too because of the dbo prefix, discussed by other posters here.
Regards, Yelena Varshal
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 1:20 PM
Points: 2,573,
Visits: 1,531
|
|
|
|
|