How to get the database name of the current SQL

Source: Internet
Author: User
Tags variables
Data | database *
Tofu production is a boutique
Http://www.asp888.net Tofu Technology Station
If reproduced please retain the copyright information
*/

The original user question is as follows:
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
Tell me the question.
(1) First of all, the current DataBase must be used for system stored procedures, which is quite certain,
I naturally thought of it. sp_who execution, listing all the current system processes all the information, I see a Runnable, there are slepping certainly should be
Runnable, a look at his spid to 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 thinking problem of T-SQL programming, first of all I do not agree to use a rule to judge two stored procedures, because this does not
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 thought of the cursor cursor, which was finally tested and successful.



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.