Here are a few questions about SQL SERVER that I would like to ask you (server-side script programming):
(1) How to obtain the current database name using SQL statements in SQL SERVER?
(2) How to define a global variable instead of a local variable, because I defined a loop here
, in which fields are added to a temporary table and data is inserted into the temporary table
operate. But these two operations must be placed in different batches (need to be separated with a GO statement).
The variable parameters controlling the loop will not work at this time, because the variable parameters can only work in one
batch and will be redefined in the next batch. What a headache! Can I define
a global variable so that it works in all batches?
These two problems are not actually universal, but the ideas and methods to solve these two problems are universal, so I will specifically talk about these two problems
( 1) First of all, it is very certain that the current DataBase needs to use system stored procedures. I naturally thought that after sp_who is executed, all the information of all current system processes is listed. When I see that there is Runnable and Slepping, I am sure. It should be runnable. After looking at its spid, I remembered that there is a system global variable @@SPID. Try it first, sp_who @@spid. Haha, it worked. 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 t-sql programming idea. First of all, I don’t agree with using one rule to judge two stored procedures, because This does not comply with the specifications of t-SQL programming, but since the problem has arisen, we must find a way to solve it. We know that it is impossible to share data in the two batches, so I thought of the cursor. After testing, it worked. It worked.