July 3, 2015 at 11:43 pm
Hi guys,
I am currently going through T-SQL examples as I am still learning SQL, please can someone tell me why we use the following line below all the time when creating tables? Also what does the 'N' mean? Also what 'OBJECT_ID' is it looking for?
IF OBJECT_ID(N'Sales.MyOrders', N'U')IS NOT NULL DROP TABLE Sales.My.Orders;
Thank you in Advance
July 4, 2015 at 12:16 am
General purpose of it is to return object ID based on given name but we use it to check if the object already exists or not before we attempt to drop it or create it. If OBJECT_ID will return NULL for an object name that means it does not exists and vice versa.
N'some string here' - this N next to the string enclosed in single quotes mean that the string is NVARCHAR type if N wouldn't be there it would be VARCHAR
IF OBJECT_ID('TestTable', 'U') IS NOT NULL
DROP TABLE TestTable
SELECT N'test string' as ntest, 'test string' test INTO TestTable
SELECT OBJECT_ID('TestTable') ObjectID, OBJECT_NAME(OBJECT_ID('TestTable')) ObjectName
EXEC sp_help 'TestTable'
IF OBJECT_ID(N'TestTable', N'U') IS NOT NULL
DROP TABLE TestTable
July 5, 2015 at 6:41 am
Many Thanks for your reply
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply