how to use SET ANSI_NULLS ON in a script wich creates several sprocs?

Pablo gil 86 Reputation points
2021-02-02T14:36:23.113+00:00

I have seen this script in docs. why does it repeat SET ANSI_NULLS ON and SET QUOTED_IDENTIFIER ON
before each batch that creates a sproc?. Is it necessary at all repeating them? or just the first ones would work the same?

USE [master]
GO

CREATE DATABASE [MySchool]
GO

USE [MySchool]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[CourseExtInfo] @CourseId int
as
select c.CourseID,c.Title,c.Credits,d.Name as DepartmentName
from Course as c left outer join Department as d on c.DepartmentID=d.DepartmentID
where c.CourseID=@CourseId

GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[DepartmentInfo] @DepartmentId int,@CourseCount int output
as
select @CourseCount=Count(c.CourseID)
from course as c
where c.DepartmentID=@DepartmentId

select d.DepartmentID,d.Name,d.Budget,d.StartDate,d.Administrator
from Department as d
where d.DepartmentID=@DepartmentId

GO

system.data.sqlclient.sqlcommand

Developer technologies Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Tom Phillips 17,771 Reputation points
    2021-02-02T17:19:36.797+00:00

    Those are default values produced by SSMS when you script objects. They are not needed at all, and not needed to be duplicated between procs.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-02-02T22:26:09.157+00:00

    Just to expand on Tom's reply. These settings are saved with the procedure definition and will override the run-time setting. This is why SSMS includes them in the script. And I guess that since they are properties of the procedure, SSMS scripts them once for every procedure - just in case you would like to change for one of them.

    But you to want to these settings. The OFF settings are legacy settings and should never be used.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.