What happened after variable declaring?

Juliana Zhou 21 Reputation points
2023-01-15T19:39:09.8666667+00:00

I just execute below syntax; will it alter our database? How can I remove all the variables?

USE Model

GO

DECLARE @date date = '04-18-2020';

DECLARE @curent_date date = getdate();

DECLARE @duedate date = getdate()+2;

SELECT @date AS 'Date', @curent_date AS 'Curent date', @duedate AS 'Due date';

GO

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,808 questions
0 comments No comments
{count} votes

Accepted answer
  1. CosmogHong-MSFT 23,321 Reputation points Microsoft Vendor
    2023-01-16T02:13:21.4666667+00:00

    Hi @Juliana Zhou

    will it alter our database?

    No, it won't. Refer to this doc for more details of local variables.

    A Transact-SQL local variable is an object that can hold a single data value of a specific type. Variables in batches and scripts are typically used:

    • As a counter either to count the number of times a loop is performed or to control how many times the loop is performed.
    • To hold a data value to be tested by a control-of-flow statement.
    • To save a data value to be returned by a stored procedure return code or function return value.

    How can I remove all the variables?

    Just select the DECLARE lines and press Backspace. Or you could DECLARE the same variables in another batch (using the batch separator 'GO'), like this:

    GO
    DECLARE @date date = '04-18-2020';
    DECLARE @curent_date date = getdate();
    DECLARE @duedate date = getdate()+2;
    SELECT @date AS 'Date', @curent_date AS 'Curent date', @duedate AS 'Due date';
    GO
    DECLARE @date date = '04-18-2020';
    DECLARE @curent_date date = getdate();
    DECLARE @duedate date = getdate()+2;
    SELECT @date AS 'Date', @curent_date AS 'Curent date', @duedate AS 'Due date';
    GO
    

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 101.8K Reputation points MVP
    2023-01-15T19:45:59.5366667+00:00

    I'm afraid that I don't really understand the question. But, no, that script will not alter any database.

    As for the variables, I guess you can select these four lines in SSMS and press the Delete button, and they will be gone.

    It may be better if you explain what you really want to achieve?

    By the way, the model database is not one you usually hang around in. As a matter of fact, you better stay out of that database, because of there is a process in that database, this prevents the creation of other databases.

    0 comments No comments