How to get the database name of the current SQL

Source: Internet
Author: User
Here are a few questions about SQL Server that you want to ask (server-side scripting):
(1) How do I get the current database name with SQL statements in SQL Server?
(2) How to define a global variable instead of a local variable, because I define the
A loop that has a temporary table plus fields and inserts data into a temporary table in this loop
The operation. However, these two operations must be placed in different batches. (You need to separate it with the GO statement).
The variable parameters of the control loop do not work, because variable parameters can only be used in a
The batch works, and the next batch is redefined. That's a headache! Can you define
global variable, so that it works in all batches?
These two problems, in fact, do not have a certain universality, but the ideas and methods to solve these two problems have a certain universality, so I specifically put these two issues out to talk about
(1) First of all, this current DataBase must use the system stored procedure this is very certain, I naturally thought of the sp_who execution, listed all the current system processes all the information, I see a Runnable, there are slepping definitely should be Runna BLE, a look at his spid think of it, there is a system of global variables @ @SPID, first try, sp_who @ @spid haha success the solution to this problem is to be familiar with the system's stored procedures and system global variables, and be good at observing and thinking
(2) As for the second question, it is purely a question of the thinking of T-SQL programming, first of all I do not agree with a rule to judge two stored procedures, because this does not conform to and T-SQL programming specifications, but since the problem, we have to find a way to solve, we know that in two batches is impossible to share the data, so I think To the cursor cursor, finally tested, also succeeded.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.