I'd like to request the servicename of an SQL Server. Usually I do this with
SELECT ##SERVICENAME
But this don't work's on Azure hosted Databases. I'm looking for a way to determine the servicename, with a try catch fallback if I'm connected to a azure database. The usual try catch works not for me - due to compile error for the statement. How can i catch this compile error ?
This statement works in both environments without compile error:
DECLARE #T TABLE(ServiceName NVARCHAR(255));
DECLARE #Rows INT
IF SERVERPROPERTY('Edition') != 'SQL Azure'
BEGIN TRY
INSERT #T(ServiceName) EXEC ('SELECT ##SERVICENAME')
END TRY
BEGIN CATCH
END CATCH
SELECT #Rows = COUNT(*) FROM #T
IF #Rows = 0
INSERT #T(ServiceName) Values ('SQL Azure')
SELECT * FROM #T
On Local Server:
On Azure:
USE ##version global variable instead.
DECLARE #version VARCHAR(200)
SELECT #version = SUBSTRING(##version, 15,5)
PRINT #version
Hope this helps.
Related
I have a SQL Server function and I want to convert to Oracle function.
This is my function:
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[SUB_FOLDERS]( #GRUP_GRUP NVARCHAR(30),#STATU NVARCHAR(30))
RETURNS #Strings TABLE (GRUP_KODU nvarchar(100))
AS
BEGIN
DECLARE #sonuc nvarchar(4000)
DECLARE merge_cursor CURSOR FAST_FORWARD FOR WITH temptab(id)
AS
( SELECT root.GRUP_KODU FROM GRUP_TABLE root WHERE GRUP_KODU=#GRUP_GRUP AND DURUM=#STATU
UNION ALL
SELECT sub.GRUP_KODU FROM GRUP_TABLE sub, temptab super WHERE sub.GRUP_GRUP = super.id )
SELECT * FROM temptab OPEN merge_cursor WHILE ##FETCH_STATUS = 0
BEGIN
SET #sonuc=''
FETCH NEXT FROM merge_cursor INTO #sonuc if (#sonuc<>'')
INSERT INTO #Strings VALUES (#sonuc)
END
RETURN
END
GO
When I run it on SQL Server it works properly but on Oracle, it gives error.
Error report:
SQL Command: functıon SUB_FOLDERS
Failed: ORA-24344: success with compilation error
24344. 00000 - "success with compilation error"
*Cause: A sql/plsql compilation error occurred.
*Action: Return OCI_SUCCESS_WITH_INFO along with the error code
Thanks in advance.
I have started a SQL Server locally on my computer. Now I am trying to find a way to modify error messages I get in SSMS.
Well you can do a TRY-CATCH. Something like the following:
SET NOCOUNT ON
CREATE TABLE #tmp(TestCol varchar(20))
INSERT INTO #tmp VALUES('test')
BEGIN TRY
INSERT INTO #tmp VALUES('testtesttesttesttesttest')
END TRY
BEGIN CATCH
DECLARE #nErrorNum INT;
SELECT #nErrorNum = ERROR_NUMBER()
IF #nErrorNum = 8152
BEGIN
RAISERROR('String is too long',10,1);
END
ELSE
THROW
END CATCH
I have to develop a WinCC Visual Basic Script management application. In this application I read an XML archive, and after that I put the information in a SQL database through a SQL INSERT query.
My problem is that I don't know how to do the error handling to view the SQL errors in VBScript MsgBox for example.
Activating the error handling with On Error Resume Next and after the evaluation of these errors with If Err.Number <> 0 Then ... the errors produced in SQL Server don't appear in VBScript.
If you want to get SQL Server error, You can use stored procedure with transaction to insert data into table:
create procedure dbo.InsertTable (
#param1 nvarchar(80)
,#param2 nvarchar(80)
,#error_text nvarchar(400) output)
as
begin
begin tran
begin try
insert into YourTable (column1, column2)
values (#param1, #param2)
end try
begin catch
set #error_text = error_message()
rollback
return
end catch
commit
end
Now You will get eventually error from the output parameter #error_text
declare #error_text nvarchar(400)
exec dbo.InsertTable 'Value1','Value2', #error_text output
select #error_text
I am trying to deploy SSIS project into SSISDB using T-SQL. Say in case of any error while deploying, error messaged got logged into catalog.operation_messages view.
Now if I execute same deploy statement in Explicit SQL transaction and if any error occurs at time of deployment I am not able to find error logged into catalog.operation_message.
Ex.
BEGIN
BEGIN TRY
BEGIN TRAN TRAN1
Declare #folder_id bigint
EXEC SSISDB.catalog.create_folder #folder_name='test1', #folder_id=#folder_id OUTPUT
Select #folder_id
EXEC SSISDB.catalog.set_folder_description #folder_name='test1', #folder_description='test1'
--Deploy
DECLARE #ProjectBinary as varbinary(max)
DECLARE #operation_id as bigint
Set #ProjectBinary = (SELECT * FROM OPENROWSET(BULK 'C:\Test\MyProject.ispac', SINGLE_BLOB) as BinaryData)
Exec SSISDB.catalog.deploy_project #folder_name = 'test1', #project_name = 'ABC', #Project_Stream = #ProjectBinary, #operation_id = #operation_id out
COMMIT TRAN TRAN1
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE()
COMMIT TRAN TRAN1
END CATCH
END
This return me error message something like
Failed to deploy project. For more information, query the operation_messages view for the operation identifier '34704'.
But when I query view and try looking for same I am not able to find any message logged by this operation_Id, is it rollbacked? if yes by which process? how I can retain it?
Help me in understanding it.
Is it natural that SQL Server does not catch objects dependencies in stored procedures through dynamic SQL:
CREATE PROCEDURE testSp (#filter nvarchar(max)) AS
exec ('select * from testTable where 1=1 AND '+ #filter)
Here SQL Server will not detect dependency between testTable and testSp.
What kind of "advice" do you have for the DBMS? I propose it could be very "cheap query" :
CREATE PROCEDURE testSp (#filter nvarchar(max)) AS
-- cheap query like 'select top 1 #id=id from testTable'
exec ('select * from testTable where 1=1 AND '+ #filter)
So the question is which queries could be good candidates for that purpose?
P.S. Of course I expect that they all will have their minuses..
When using dynamic SQL the query parts that are tekst (between quotes) are not detected as code by the IDE or the engine until the moment they are excuted. So this answers your first question, yes it is natural.
The only way around this that I can think of is to create a view using the generated output of the dynamic sql and check if the view definition is still valid at any point you want to check if the procedure is valid.
Usually when you need to do something like this there is an earlier departure from standard methods that if handled removes the need for such silly tricks.
Example:
USE demo
GO
DECLARE #sql NVARCHAR(MAX) = '
SELECT firstname, lastname FROM dbo.employees'
DECLARE #view NVARCHAR(MAX) = '
CREATE VIEW dbo.test_view
AS ' + #sql
EXEC sp_executesql #view
BEGIN TRY
DECLARE #validation int = (SELECT TOP 1 COUNT(*) FROM demo..test_view)
EXEC sp_executesql #sql
END TRY
BEGIN CATCH
PRINT 'Dynamic SQL out of date'
END CATCH
SET NOEXEC ON
select * from testTable
SET NOEXEC OFF
do the job: code really not executed, but dependecy is declared.