I'm working on migrating SQL Server 2008 R2 database to SQL Server 2014. Having trouble with the following trigger. Looks like something with RAISEERROR is not supported in newer version.
ALTER TRIGGER [dbo].[Route_ITrig]
ON [dbo].[Route]
FOR INSERT AS
/*
* PREVENT NULL VALUES IN 'RouteName'
*/
IF (SELECT Count(*) FROM inserted WHERE RouteName IS NULL) > 0
BEGIN
RAISERROR 44444 'Field ''RouteName'' cannot contain a null value.'
ROLLBACK TRANSACTION
END
This is the error I'm getting
Msg 102, Level 15, State 1, Procedure Route_ITrig, Line 15
Incorrect syntax near '44444'
This is a SQL function, hence all required variables must be passed in brackets, as follows:
RAISERROR(44444, 'Field ', 'RouteName', ' cannot contain a null value.')
Related
I'm trying to execute the following query:
CREATE PROCEDURE GetValues #ValueName nvarchar(256)
AS
WITH tmpTable AS
(
SELECT *
FROM myTable
WHERE (Value = #ValueName AND Message IS NOT NULL)
)
SELECT
Message, COUNT(Message)
FROM
tmpTable
GROUP BY
Message
GO;
EXEC GetValues #ValueName = 'myvalue';
When I do I'm getting a following error:
Msg 102, Level 15, State 1, Procedure GetValues, Line 11 [Batch Start Line 0]
Incorrect syntax near ';'
I can't understand where the error is coming from. Can someone help?
I'm running this query in SQL Server Management Studio.
GO is not a SQL command.
Only SQL command can require a ";"
GO is a command destinate to be executed by SSMS and not by SQL Server.
GO is a separator of batch commands.
Go says to SSMS throw the command to the SQL Server and when it returns, continue to throw the commands until a new GO statement is find.
I'm trying to write a query to gather memory stats across my multiple SQL editions that are SQL 2008 R2, SQL 2012, SQL 2014, SQL 2016.
Due to the column name changes in sys.dm_os_sys_info I'm trying to get around that, but both of the queries fail with:
Msg 207, Level 16, State 1, Line 7
Invalid column name 'physical_memory_kb'.
Any ideas?
IF (LEFT(cast(serverproperty('productVersion') as varchar(100)),2) = '10')
BEGIN
select physical_memory_in_bytes/1048576 FROM sys.dm_os_sys_info
END
ELSE
BEGIN
select physical_memory_kb/1024 FROM sys.dm_os_sys_info
END
select
CASE LEFT(cast(serverproperty('productVersion') as varchar(100)),2)
WHEN 10 Then physical_memory_in_bytes/1048576
ELSE physical_memory_kb/1024
END
FROM sys.dm_os_sys_info
The error you've got is compilation error, i.e. parser tries to figure out the columns of existing tables/views and it doesn't consider execution flow (IF)
You can use this code for your purpose:
IF (LEFT(cast(serverproperty('productVersion') as varchar(100)),2) = '10')
BEGIN
exec('select physical_memory_in_bytes/1048576 FROM sys.dm_os_sys_info')
END
ELSE
BEGIN
exec ('select physical_memory_kb/1024 FROM sys.dm_os_sys_info')
END
You can't put a conditional statement to select from non-existent columns like that, the compiler will still validate all the statements within the select clause.
The first conditional works because it is two distinct select statements.
I would suggest using the first conditional only, if you need additional info then select the physical_memory_in... column into a temp table or table variable and then join that into your main query.
When I try to set the compatibility level for SQL Server in a script it errors out for SQL Server 2008. I want this script to be same to executed against all the servers - 2008, 2012 or 2014 which we currently support.
-- Set database compatibility --
IF SUBSTRING(CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR),1 ,2) >= 11
BEGIN
RAISERROR ('sql server 12 and above', 10, 1)
ALTER DATABASE JJAI_TESTDB SET COMPATIBILITY_LEVEL = 110
END
ELSE
BEGIN
RAISERROR ('sql server 2008',10, 1)
ALTER DATABASE JJAI_TESTDB SET COMPATIBILITY_LEVEL = 100
END
GO
Below is the error:
Msg 15048, Level 16, State 1, Line 5
Valid values of the database compatibility level are 80, 90, or 100.
Okay, I was able to do it by executing the alter statement within an execute statement like below.
/* Set database compatibility */
IF SUBSTRING(CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR),1 ,2) >= 11
EXECUTE ('ALTER DATABASE $(DATABASENAME) SET COMPATIBILITY_LEVEL = 110')
ELSE
EXECUTE ('ALTER DATABASE $(DATABASENAME) SET COMPATIBILITY_LEVEL = 100')
GO
Is there any way to find the SQL query errors without executing the query in SQL server?
For example,
Use TestDB
Select * from Employee1
Consider TestDB doesn’t contain any table named “Employee1”. In this case, while we running this query, we will get the “Invalid object name Employee1” SQL error.
My question is that, after writing the query, is there any better approach to find these errors without executing?
I got a comment from my colleague is that use the sp_describe_first_result_set System stored procedure.
If you have 2012 and above you can use the sp that your colleague has mentioned.
If you use 2008 R2 and less then use
SET FMTONLY ON;
Select * from MyTabl;
SET FMTONLY OFF;
Msg 208, Level 16, State 1, Line 3
Invalid object name 'MyTabl'.
This will execute the query but not process any data. NOEXEC only parses the query so any objects that don't exist will not be flagged. NOEXEC checks syntax only. So what you want is either sp_describe_first_result_set or FMTONLY.
Alternatively if you have a select then you can also append to your where 1=2. For example
SELECT *
FROM MyTabl
WHERE 1=2
which gives them same result
Msg 208, Level 16, State 1, Line 3
Invalid object name 'MyTabl'
I am using SQL Server 2008. I have tried to execute the following:
BEGIN TRY
SELECT 1/0;
END TRY
BEGIN CATCH
PRINT 'ERROR'
END CATCH;
But I am getting the following error:
>Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'TRY'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'END'.
Can any one tell me how execute try catch in SQL Server?
That is a completely valid statement for SQL Server 2005 and up, so I'd check your compatibility level using sp_dbcmptlevel (Transact-SQL):
exec sp_dbcmptlevel 'YourDatabaseName'
80 = SQL Server 2000
90 = SQL Server 2005
100 = SQL Server 2008
I think it will return 80 or lower, it seems that it doesn't know BEGIN TRY, only BEGIN. The BEGIN TRY was added in SQL Server 2005.
If you still have this error, the question becomes, have you put a BEGIN CATCH...END CATCH section in. If not, you get this error. If you have a BEGIN TRY...END TRY section with no T-SQL code in it, then it will also produce the error.
I don't think it's helpful people tell you that you're using SQL Server 2000. It is more likely a T-SQL coding issue as you tend to know what server you are running.
Begin try
Begin transaction
--------
--------
Commit transaction
End try
Begin catch
Rollback transaction
End catch
http://intquesans.blogspot.com/2011/05/how-we-can-use-try-catch-in-sql.html