SQLDependency throwing thousands of invalid notifications with stored proc - sql

I've built a vb.net service to update Exchange appointments as appointment changes are made in our accounting suite. Everything works fine if I run the SQL query straight from VB.net but if I reference a stored proc with the exact same code I receive thousands of "invalid" notifications. I was wondering if anyone could tell me why?
I'm using VS2012, SQL Server 2008, and .NET 4.0
I pass each "option" into the query string and execute it before starting the dependency with this query:
select Dispatch_Id, Schedule_Time, Dispatch_Time, Arrival_Time, Departure_Time from dbo.Ticket_Dispatch
The stored proc is:
USE [database]
GO
/****** Object: StoredProcedure [dbo].[watch] Script Date: 1/22/2014 5:28:28 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
ALTER PROCEDURE [dbo].[watch]
-- Add the parameters for the stored procedure here
AS
BEGIN
SET ANSI_NULLS ON;
SET ANSI_PADDING ON;
SET ANSI_WARNINGS ON;
SET CONCAT_NULL_YIELDS_NULL ON;
SET QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT ON;
SET ARITHABORT ON;
select Dispatch_Id, Schedule_Time, Dispatch_Time, Arrival_Time, Departure_Time from dbo.Ticket_Dispatch
END
Before any of this I test for permissions. The user has ownership over the table and the proc so there shouldn't be any problems there.

Related

Update stored procedure using C#?

I have 10 database servers and most procedures are the same.
So I plan to make procedure distribute program.
For convenient application, I want to use "the procedure modify code made by tool" itself.
For example, when I click modify button of the procedure on SSMS the code is like below.
USE [DB]
GO
/****** Object: StoredProcedure [dbo].[HongTestProcedure] Script Date: 2020-08-28 오전 11:09:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: hong
-- Create date: 2020-03-07
-- =============================================
ALTER PROCEDURE [dbo].[HongTestProcedure]
-- Add the parameters for the stored procedure here
#ID varchar(10)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT *
FROM table
END
And then, if I want to modify the parameter of the procedure.
Maybe the code is like below:
USE [DB]
GO
/****** Object: StoredProcedure [dbo].[HongTestProcedure] Script Date: 2020-08-28 오전 11:09:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: hong
-- Create date: 2020-03-07
-- =============================================
ALTER PROCEDURE [dbo].[HongTestProcedure]
-- Add the parameters for the stored procedure here
#ID varchar(10)
#ID2 varchar(10) -- it is added.
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT *
FROM table
END
I consider that program has a textbox and button.
and I want to insert all above code.
maybe it looks like it:
So, I try the code like below:
public bool UpdateProcedure(string dbip, string query)
{
DataSet ds = new DataSet();
SqlConnection sqlConn = new SqlConnection("server = " + dbip + dbInfo);
sqlConn.Open();
SqlCommand sqlComm = new SqlCommand(query, sqlConn);
sqlComm.ExecuteNonQuery();
return true;
}
If it run successfully, I will run query on 10 database servers.
But it return error message. even it can be run in SSMS.
incorrect syntax near 'GO'
CREATE/ALTER PROCEDURE must be the first statement in a query batch
Can I solve it?
Yes. Since you can construct it as an Dynamic SQL in C# and Open an SQL Connection and pass the Dynamic SQL to EXEC sp_executesql #DynamicSQL. This will create the required SP in the required Database.

disable automatic auto code formatter sql server

I create a stored procedure like this in ssms:
USE [UnitTest]
GO
/****** Object: StoredProcedure [dbo].[ProcedureName] Script Date: 2017-09-18 15:30:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ProcedureName]
#p1 INT=0, #p2 INT=0
AS
BEGIN
SET NOCOUNT ON;
SELECT #p1,
#p2;
END
GO
but when I save the stored procedure and reopen it, automatically ssms changes the format like this:
USE [UnitTest]
GO
/****** Object: StoredProcedure [dbo].[ProcedureName] Script Date: 2017-09-18 15:29:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ProcedureName]
#p1 INT=0,
#p2 INT=0
AS
BEGIN
SET NOCOUNT ON;
SELECT #p1, #p2;
END
GO
how can I disable this feature?
in Sql Management Studio to disable SQL autoformat when typing brackets:
Go To menu Tools --> Options --> Text Editor --> Transact SQL --> General and disable the checkbox AutoList Members
Im this menu you can find similar action sthat may solve your specific question

How to add header comments when altering stored procedures in SQL Server

I have a stored procedure where there are no header comments. I want to add them, but whenever I try, it is not included.
In SQL Server Management Studio I :
1.Right-click my stored procedure and click modify
USE [ABigDB]
GO
/****** Object: StoredProcedure [dbo].[spDoWork] Script Date: 21/08/2015 14:11:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spDoWork]
#Id uniqueidentifier,
#Session nvarchar(50),
#XMLData xml
WITH EXECUTE AS OWNER
AS
BEGIN
--etc etc...
END
2.I paste comments above the stored procedure and run the script :
-- Stored Procedure
-- Author: Dave
-- Create date: 21/08/2015
-- Description: Does Stuff
-- Change history
-- 07/08/2015 - Overlord - Done stuff
-- 06/08/2015 - Kerrigan - Done more stuff
USE [ABigDB]
GO
/****** Object: StoredProcedure [dbo].[spDoWork] Script Date: 21/08/2015 14:11:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spDoWork]
#Id uniqueidentifier,
#Session nvarchar(50),
#XMLData xml
WITH EXECUTE AS OWNER
AS
BEGIN
--etc etc...
END
3.When I modify the same stored procedure it appears as :
USE [ABigDB]
GO
/****** Object: StoredProcedure [dbo].[spDoWork] Script Date: 21/08/2015 14:11:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spDoWork]
#Id uniqueidentifier,
#Session nvarchar(50),
#XMLData xml
WITH EXECUTE AS OWNER
AS
BEGIN
--etc etc...
END
So how do I get the comments to appear there?
I solved it by doing the following:
USE [ABigDB]
GO
/****** Object: StoredProcedure [dbo].[spDoWork] Script Date: 21/08/2015 14:11:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Stored Procedure
-- Author: Dave
-- Create date: 21/08/2015
-- Description: Does Stuff
-- Change history
-- 07/08/2015 - Overlord - Done stuff
-- 06/08/2015 - Kerrigan - Done more stuff
ALTER PROCEDURE [dbo].[spDoWork]
#Id uniqueidentifier,
#Session nvarchar(50),
#XMLData xml
WITH EXECUTE AS OWNER
AS
BEGIN
--etc etc...
END
Consider using the meta data in addition to your procedures, tables, columns, etc. for documentation purposes.
See the following that helps when reviewing your db objects.
Is it possible to add a description/comment to a table in Microsoft SQL 2000+

SET XACT_ABORT ON not Worked in Create Procedure

I use SQL Server 2008 SP3 (10.0.5500) And I have some Problems with Rollback Transactions, at first I need to know something. This is My create Procedure script:
USE [MYDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET XACT_ABORT ON
GO
If Exists(Select * From Sys.Objects Where [object_id]=Object_Id(N'[Members].[MyProc]') And ObjectProperty([object_id], N'IsProcedure') = 1)
Begin
Drop Procedure [Members].[MyProc]
Print 'Procedure [Members].[MyProc] Dropped!'
End
GO
CREATE PROCEDURE [Members].[MyProc](
....
)
AS
BEGIN
BEGIN TRANSACTION [MyProcCHK]
....
COMMIT TRANSACTION [MyProcCHK]
END
GO
And After run this script, I check Procedure from: MyDB ->Programmability->StoredProcedures And Click to Modify [Members].[MyProc] Then this is the script shown:
USE [MYDB]
GO
/****** Object: StoredProcedure ... ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [Members].[MyProc](
....
)
AS
BEGIN
BEGIN TRANSACTION [MyProcCHK]
....
COMMIT TRANSACTION [MyProcCHK]
END
GO
So Where is SET XACT_ABORT ON? And why that not displayed here? As I have some problems with rollback transactions in stored procedures I think the SET XACT_ABORT ON never saved. Am I right? and if yes, what is the solution? I can't use SET XACT_ABORT ON in stored procedures? or where is my fault?
You need to mention set xact_abort on inside the Create procedure statement
CREATE PROCEDURE [Members].[MyProc](
........
)
AS
SET XACT_ABORT ON
BEGIN
BEGIN TRANSACTION [MyProcCHK]
....
COMMIT TRANSACTION [MyProcCHK]
END
GO

How to Deal with SET ANSI_NULLS ON or OFF?

I want to call this procedure that sends one value that can be NULL or any int value.
SELECT DomainName, DomainCode FROM Tags.tblDomain WHERE SubDomainId =#SubDomainId
I simply want to use this single query rather than what i m doing right now in below given code.
I searched for this how could i do this then i got this Link.
According to this I have to set ANSI_NULLS OFF
I am not able to set this inside this procedure before executing my sql query and then reset it again after doing this.
ALTER PROCEDURE [Tags].[spOnlineTest_SubDomainSelect]
#SubDomainId INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
IF #SubDomainId IS NULL
SELECT DomainName, DomainCode FROM Tags.tblDomain WHERE SubDomainId IS NULL
ELSE
SELECT DomainName, DomainCode FROM Tags.tblDomain WHERE SubDomainId =#SubDomainId
END
What will be the better practice to do deal with ANSI_NULLS or Using If Else
SET ANSI_NULLS is ony defined at stored proc create time and cannot be set at run time.
From CREATE PROC
Using SET Options
The Database Engine saves the settings
of both SET QUOTED_IDENTIFIER and SET
ANSI_NULLS when a Transact-SQL stored
procedure is created or modified.
These original settings are used when
the stored procedure is executed.
Therefore, any client session settings
for SET QUOTED_IDENTIFIER and SET
ANSI_NULLS are ignored when the stored
procedure is running. Other SET
options, such as SET ARITHABORT, SET
ANSI_WARNINGS, or SET ANSI_PADDINGS
are not saved when a stored procedure
is created or modified. If the logic
of the stored procedure depends on a
particular setting, include a SET
statement at the start of the
procedure to guarantee the appropriate
setting. When a SET statement is
executed from a stored procedure, the
setting remains in effect only until
the stored procedure has finished
running. The setting is then restored
to the value the stored procedure had
when it was called. This enables
individual clients to set the options
they want without affecting the logic
of the stored procedure.
The same applies to SET QUOTED_IDENTIFIER
In this case, use IF ELSE because SET ANSI_NULLS will be ON in the future.
Or Peter Lang's suggestion.
To be honest, expecting SubDomainId = #SubDomainId to work when #SubDomainId is NULL is not really correct usage of NULL...
Can't you use a single query?
SELECT DomainName, DomainCode
FROM Tags.tblDomain
WHERE ( #SubDomainId IS NULL AND SubDomainId IS NULL )
OR ( SubDomainId = #SubDomainId )
FYI, I'm pretty sure ...
ANSI_NULLS OFF
Applies to the procedure when you create/edit it, it's like a setting of the procedure.
So either the procedure has it ON or OFF. Your example was a query not a procedure so I'm a little confused.
But if you have SQL 2005/2008 for example if you "edit" procedure it opens up your procedure in a new tab you'll see the ANSI_NULLS OFF near the top.
You can edit it there and set it ON or OFF and update it to change ...