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
ASkeywords that you had – just define your table variable like this:When trying to run your sample #2 on my SQL Server 2008 R2 (SP1 – Developer Edition), I get these errors:
I have to remove the
ASkeywords to make it work. UsingASis trying to define an alias for the column – but since thatUNIQUEIDENTIFIERorINTis 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 unnecessaryASkeywords