10
IF OBJECT_ID (N'dbo.AWBuildVersion', N'U') IS NOT NULL
DROP TABLE dbo.AWBuildVersion;

I don't understand what N and U are for in SQL server ?

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bpoon
  • 109
  • 1
  • 1
  • 3
  • Possible duplicate: http://stackoverflow.com/questions/10025032/what-is-the-meaning-of-the-prefix-n-in-t-sql-statements – valverij Nov 16 '14 at 15:41
  • For `'U'`, see the [`OBJECT_ID`](http://msdn.microsoft.com/en-us/library/ms190328.aspx) documentation, which links in turn to the [`sys.objects`](http://msdn.microsoft.com/en-us/library/ms190324.aspx) documentation. –  Nov 16 '14 at 15:42
  • 1
    I personally would recommend rewriting this query as `IF EXISTS (SELECT * FROM sys.tables WHERE Name = N'AWBuildVersion')` - use the more focused `sys.tables` and so on catalog views to detect existance of an object – marc_s Nov 16 '14 at 15:43

2 Answers2

11
  • U stands for Table (user-defined). Check here for more info. The syntax OBJECT_ID is

    OBJECT_ID ( '[ database_name . [ schema_name ] . | schema_name . ] 
    object_name' [ ,'object_type' ] )
    

    here Object_type = 'U' which denotes Table (user-defined)

  • N Makes the String to be considered as nvarchar data type. It denotes that the subsequent string is in Unicode (the N actually stands for National language character set). Which means that you are passing an NCHAR, NVARCHAR or NTEXT value, as opposed to CHAR, VARCHAR or TEXT.
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
4

'U' character stands for itself. The N prefix makes it a one-character UNICODE string. OBJECT_ID procedure expects you to pass one of pre-defined one-character values for the second parameter, which needs to be a UNICODE string.

This syntax is used to create literals with characters in other encodings, for example

CREATE TABLE hello_world (str NVARCHAR(20))
INSERT INTO hello_world (str) VALUES (N'Здравствуй, мир!')
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    I thought `U` stands for `User table` – M.Ali Nov 16 '14 at 15:51
  • 3
    No, it stands for 'U' which is a paramter for the function called. WHat it means is not what it stands for - the MEANING of passing an U in this parameter is... documented... in the documentation. – TomTom Nov 16 '14 at 15:59
  • 2
    @TomTom You should have made it explicit that you are replying to M.Ali. It looks like people misinterpret your comment as a disagreement with my answer. – Sergey Kalinichenko Nov 16 '14 at 16:16