Using SQL Server temporary tables skillfully

Source: Internet
Author: User
Tags exit log sql

SQL Server temp tables are often needed, and here's how you can use SQL Server temp tables to troubleshoot problems that prevent users from recurring logins for your information.

When we develop business software, we often encounter the question of how to prevent users from repeatedly logging on to our system. Especially for the bank or the financial department, it is to restrict the user to log in as their work number.

Some people may say that in the User Information table, add a field to determine the status of user work number login, write 1 after login, write 0 when you exit, and when you log in to determine whether the sign is 1, if you do not let the user work number login. But this is bound to bring new problems: such as a power outage, such as unpredictable phenomenon, the system is abnormal exit, can not be the flag location of 0, then the next time the user work number login can not log in, this How to do?

Maybe we can change the train of thought: Is there anything that can be automatically recycled by the system after the connection is disconnected? By the way, SQL Server Temp table has this feature! But we can't use local SQL Server temp tables here. Because a local temporary table is a separate object for each connection, we can only use the global temporary table to achieve our goal.

Well, as the situation is clear, we can write a simple stored procedure like the following:

 
 
  1. CREATE PROCEDURE Gp_findtemptable
  2. /* Look for a global temporary table named with the operation employee number
  3. * If not, place the out parameter to 0 and create the table, and if so, set the out parameter to 1
  4. * The global temporary table will be automatically recycled by SQL Server after the connection is disconnected
  5. * In the event of a power outage, the global temporary table exists in tempdb,
  6. But they've lost their activity.
  7. * Use the OBJECT_ID function to judge that it does not exist.
  8. */
  9. @v_userid varchar (6),--Operation employee number
  10. @i_out int out--output parameter 0: No login 1: Already logged in
  11. As
  12. DECLARE @v_sql varchar (100)
  13. If object_id (' tempdb.dbo.## ' "' + @v_userid) is null
  14. Begin
  15. Set @v_sql = ' CREATE TABLE # # ' + @v_userid +
  16. "(UserID varchar (6)) '"
  17. EXEC (@v_sql)
  18. Set @i_out = 0
  19. End
  20. Else
  21. Set @i_out = 1

In this process, we see that if a global temporary table named with a user name does not exist, the procedure creates one and puts the out parameter to 0, and if it already exists, the out parameter is set to 1.

So, when we call this procedure in our application, if we get an out parameter of 1 o'clock, we can just jump out of a message and tell the user, "Sorry, this work is being used!"




Related Article

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.