How to list out all Foreign Keys with “WITH NOCHECK” in SQL Server

The following will return the name of the foreign keys in the current database that are disabled i.e. WITH NOCHECK For SQL Server 2005/2008: select * from sys.foreign_keys where is_disabled=1 There was some discussion in the answer about the difference between disabled & not trusted. What’s below explains the differnce Here’s some code to clarify … Read more

SQL Server “AFTER INSERT” trigger doesn’t see the just-inserted row

Triggers cannot modify the changed data (Inserted or Deleted) otherwise you could get infinite recursion as the changes invoked the trigger again. One option would be for the trigger to roll back the transaction. Edit: The reason for this is that the standard for SQL is that inserted and deleted rows cannot be modified by … Read more

Row_Number() with union query

Try this: SELECT *, ROW_NUMBER() OVER(ORDER BY Id) ROW_NUM FROM ( select Id, VersionNumber from documents where id=5 Union all select Id, VersionNumber from versions where id=5 ) a order by VersionNumber desc TO filter by version number 5 use: SELECT * FROM (SELECT *, Row_number() OVER(ORDER BY versionnumber DESC, id) row_num FROM (SELECT id, … Read more