简体   繁体   中英

Stored procedure for verifying backup

I am trying to write a stored procedure for verifying backups. This is what I have done so far.

I have used master to store the procedure.

USE [master]
GO
CREATE PROCEDURE verifyData @filename nvarchar(225)
AS
RESTORE VERIFYONLY FROM DISK = @filename;
GO
GO 

But after I save the procedure and check it again the file changes to

USE [master]
GO
/****** Object:  StoredProcedure [dbo].[verifyData]    Script Date: 05/10/2021 3:49:53 pm ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[verifyData] @filename nvarchar(225)
AS
RESTORE VERIFYONLY FROM DISK = @filename;

Where a red squiggly line is shown [dbo].[verifyData] here

Also when I try to execute the procedure my intellisense is not able to pick the procedure out

Any insights would be helpful

But after I save the procedure and check it again the file changes to [dbo].[verifyData] ... Also when I try to execute the procedure my intellisense is not able to pick the procedure out

SQL Server DATABASE contains multiple objects such as tables, views, stored procedures, functions, indexes, triggers. These objects owned by a logical entities named SCHEMA.

Unless specified otherwise, all Transact-SQL references to specific object can be a four-part name in the following format: [server_name].[database_name].[schema_name].object_name . This format called Four Parts Name and you can think about it like a full logical path to the object

A four-part name is in a way equivalent to a full path to a file for example: C:\Program Files\Microsoft SQL Server\MSSQL15.SQL2019\MSSQL\DATA\AdventureWorks2019.mdf

To uniquely identify an object within multi-servers environment, you need all four parts: Server Name (1), Database Name (2), Schema Name (3) and Object Name (4).

But when we are working inside a specific database, then we usually uses a two-part name (or two parts "path" if you want), which include the SHEMA and the name of the OBHECT. For example: CREATE PROCEDURE MySchemaName.MySP , or SELECT ColA FROM MySchemaName.MyTableName

A two-part name is in a way equivalent to a relative path to a file. for example if we are in the folder: C:\Program Files\Microsoft SQL Server\MSSQL15.SQL2019\MSSQL\ Then as can use only the relative path to the file DATA\AdventureWorks2019.mdf

Unfortunately most people are lazy (or do not know well) and therefore they are using the name of the object without the name of the SCHEMA, just you did when you created the Stored Procedure. This is not well formatted code!

When you create an object without explicitly using the SCHEMA name (as you did), then SQL Server uses the default schema as the owner of that object, which is [dbo]

But after I save the procedure and check it again the file changes to [dbo].[verifyData]

Off-topic by HIGHLY important. (1) We do not save object but CREATE objects. (2) This is NOT a file but a Stored Procedure.

Note. you must start use the correct phrases if you want to understand.

The answer is simple:

The server uses the right format which is two-part name [dbo].[verifyData] - Name of the default schema, which own the stored procedure + The name of the stored procedure

when I try to execute the procedure my intellisense is not able to pick the procedure

This happens since you did not started with the SCHEMA name. Start to type the name of the SCHEMA and the intellisense should find the SCHEMA name. Next you add dot . and start to type the stored procedure name and the intellisense should find that stored procedure under the SCHEMA

In summary

(1) You should always use at least two-part name (even if most us do not do it)

(2) You can and you should CREATE new SCHEMA and group your objects in the database in a logical groups. To create new SCHEMA you can use: CREATE SCHEMA [Customer] . For more information and option when you CREATE a SCHEMA, check this link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM