disable automatic auto code formatter sql server - sql

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

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.

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+

SQLDependency throwing thousands of invalid notifications with stored proc

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.

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

sql statement problem when running multiple executions simultaneously

The problem is whenever I execute the stored procedured "usp_Execute100K" in the Query Windows simultaneously the server could not locate the record and it creating a new record even though the record is already there. You have to run the "usp_Execute100K" multiple windows simultaneously to see this problem.
What I want is to make sure the server only create 1 record per day, if the record does exist then only update the "StatisticTotal" column to the next number.
This problem is taught.
The below are 1 table and two stored procedures
Statistic /Table name/
usp_Statistic_InsertOrUpdate /this will update the "StatisticTotal" column or insert if the record does not exist/
usp_Execute100K /* this will execute the usp_Statistic_InsertOrUpdate 100 thousand times*/
PLEASE COPY THE SCRIPT BELOW TO RECREATE THE TABLE AND STORED PROCEDURES.
GO
/****** Object: StoredProcedure [dbo].[usp_Execute100K] Script Date: 09/07/2011 15:37:29 ******/
DROP PROCEDURE [dbo].[usp_Execute100K]
GO
/****** Object: StoredProcedure [dbo].[usp_Statistic_InsertOrUpdate] Script Date: 09/07/2011 15:37:29 ******/
DROP PROCEDURE [dbo].[usp_Statistic_InsertOrUpdate]
GO
/****** Object: Table [dbo].[Statistic] Script Date: 09/07/2011 15:37:28 ******/
DROP TABLE [dbo].[Statistic]
GO
/****** Object: Table [dbo].[Statistic] Script Date: 09/07/2011 15:37:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Statistic](
[StatisticID] [uniqueidentifier] NOT NULL,
[StatisticAccount] [uniqueidentifier] NOT NULL,
[StatisticTotal] [float] NOT NULL,
[StatisticCreatedDate] [datetime] NOT NULL,
[DebugDate] [datetime] NULL,
CONSTRAINT [PK_Statistics] PRIMARY KEY CLUSTERED
(
[StatisticID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: StoredProcedure [dbo].[usp_Statistic_InsertOrUpdate] Script Date: 09/07/2011 15:37:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_Statistic_InsertOrUpdate](
#StatisticAccount uniqueidentifier
)
AS
SET NOCOUNT OFF; --this can be turn on or off, it has no effects
DECLARE #NowDate date
SET #NowDate=CONVERT(datetime, CONVERT(char, GETDATE(), 106))--remove all time.
--UPDATE ONLY IF IT HAS THE SAME DATE
UPDATE TOP (1) Statistic SET StatisticTotal =StatisticTotal+1 WHERE (StatisticAccount=#StatisticAccount AND StatisticCreatedDate = #NowDate)
if ##ROWCOUNT=0--If the above statement return no effects then create a new record
BEGIN
INSERT TOP (1)INTO Statistic(StatisticID, StatisticAccount, StatisticTotal, StatisticCreatedDate,DebugDate) VALUES (NEWID(),#StatisticAccount,1,#NowDate,#NowDate)
END
GO
/****** Object: StoredProcedure [dbo].[usp_Execute100K] Script Date: 09/07/2011 15:37:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_Execute100K]
AS
SET NOCOUNT OFF; --this can be turn on or off, it has no effects
declare #current float
set #current=0
while (#current <100000)
begin
--INSERT THIS STATEMENT for 100 thousand times
exec usp_Statistic_InsertOrUpdate'4c34eea5-fe17-4b11-8e06-0039577e7421'
set #current = #current + 1
end
GO
Have you considered wrapping your UPDATE/INSERT combo in a transaction? While the constraint and TRY/CATCH will certainly prevent a second row, it will discard the attempt as well. If you wrap it in a transaction, and use TABLOCK (I assume you are potentially doing a lot of writes to this table, but not a ton of reads), you hamper concurrency slightly but you can better ensure that one user will only see the state of the table after the other user is done with it. I could get two rows reliably as is, but I couldn't get two rows when I used this:
ALTER PROCEDURE [dbo].[usp_Statistic_InsertOrUpdate]
#StatisticAccount UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON; -- this should always be on
DECLARE #NowDate DATE = CURRENT_TIMESTAMP;
-- by definition, you don't need to convert to remove time from date
BEGIN TRANSACTION;
--UPDATE ONLY IF IT HAS THE SAME DATE
UPDATE dbo.Statistic WITH (TABLOCK)
SET StatisticTotal += 1
WHERE StatisticAccount = #StatisticAccount
AND StatisticCreatedDate = #NowDate;
IF ##ROWCOUNT = 0
-- If the above statement return no effects then create a new record
BEGIN
INSERT dbo.Statistic
(
StatisticID,
StatisticAccount,
StatisticTotal,
StatisticCreatedDate,
DebugDate
)
SELECT
NEWID(),
#StatisticAccount,
1,
#NowDate,
#NowDate;
END
COMMIT TRANSACTION;
END
GO
Error trapping, rollback, constraints etc. I'll leave as an exercise.
1) create a unique index on Statistic.StatisticAccount + Statistic.StatisticCreatedDate or just Statistic.StatisticCreatedDate what ever your business rules dictate.
2) wrap your INSERT WITH:
BEGIN TRY
INSERT ...
END TRY
BEGIN CATCH
END CATCH
it will just ignore/throw away the error generated by the failed duplicate insert.