This one gives the exception: 'NVARCHAR' is not a recognized built-in function name.
DECLARE @BatchIds TABLE
(
BatchId AS UNIQUEIDENTIFIER ,
UserLogonId AS NVARCHAR(80) ,
ReportStatus NVARCHAR(100) ,
Created DATETIME ,
RunTimeInMins AS INT ,
ReportName NVARCHAR(200)
)
When I remove the "AS" for NVARCHAR, it does not give any exception.
DECLARE @BatchIds TABLE
(
BatchId AS UNIQUEIDENTIFIER ,
UserLogonId NVARCHAR(80) ,
ReportStatus NVARCHAR(100) ,
Created DATETIME ,
RunTimeInMins AS INT ,
ReportName NVARCHAR(200)
)
Any suggestions as to why?
Update:
Please note that my question is more directed towards why AS works different in this situation behind the scenes than trying to solve a work situation. For those who could not get the 2nd sample working, please try this. This gives me the same data I inserted without errors and the SQL Server version as Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)....
DECLARE @BatchIds TABLE
(
UserLogonId NVARCHAR(80) ,
ReportStatus NVARCHAR(100) ,
Created DATETIME
)
INSERT INTO @BatchIds
( UserLogonId ,
ReportStatus ,
Created
)
VALUES ( 'Test1' ,
'Test2' ,
'2012-08-08'
)
SELECT *
FROM @BatchIds
SELECT @@VERSION AS [Version]
Putting an AS in this query for NVARCHAR throws the same exception as above.
UPDATE:
This is resolved. This behaviour is only when you compile it and SQL Server allows you to compile this. But when you run it, then it throws an exception for any datatype using AS.
You don't need all those AS
keywords that you had - just define your table variable like this:
DECLARE @BatchIds TABLE
(
BatchId UNIQUEIDENTIFIER ,
UserLogonId NVARCHAR(80) ,
ReportStatus NVARCHAR(100) ,
Created DATETIME ,
RunTimeInMins INT ,
ReportName NVARCHAR(200)
)
When trying to run your sample #2 on my SQL Server 2008 R2 (SP1 - Developer Edition), I get these errors:
Msg 207, Level 16, State 1, Line 3
Invalid column name 'UNIQUEIDENTIFIER'.Msg 207, Level 16, State 1, Line 7
Invalid column name 'INT'.
I have to remove the AS
keywords to make it work. Using AS
is trying to define an alias for the column - but since that UNIQUEIDENTIFIER
or INT
is a reserved keyword in SQL Server, you cannot use those as column aliases. Same for SQL Server 2012 Express - I get those errors, too, and cannot make it work unless I remove those unnecessary AS
keywords