SQL Getting Started Classic (vii) script and batch processing

Source: Internet
Author: User

What is a script. We learned in front of the Create TABLE <table name>, use <database name> These are scripts, why with scripts. Scripts are stored in files and can be reused and extracted.

Create variable: DECLARE statement most commonly used syntax: DECLARE @<variable name> <variable type>[=value][[,@<variable name> < Variable type>[=value]@<variable name> <variable type>[=value],[.....n]];

DECLARE @TotalCount int=0--Create a variable and initialize the value (SQL server2008 the following database error) Select @TotalCount--Output 0SET @TotalCount =10-- Set the variable value to 10select @TotalCount--Output 10select @TotalCount =count (*) from Dbo.test003select @TotalCount--Output 5 can also create a table. DECLARE @temp Table (  ID  int identity (primary) key,  name  nvarchar () not null) insert INTO @temp VALUES (' test_1 '), insert into @temp values (' test_2 '), select * from @temp result: Below

Using Process Control Statements:

Create a data table first

Use Pandagocreate TABLE test004 (  ID  INT Identity (primary)  key,  name  nvarchar () is not NULL,  typeint  int  )

If....else: Basic grammar If<boolean Expression><sql statement>| BEGIN <code series> END [ELSE <sql statement>| BEGIN <code series> END] where the expression can be almost any Boolean expression.

DECLARE @count int set @count =0;while (@count <100) begin set @[email protected]+1;   if (@count%4=0)      insert into dbo.test004 values (' test_ ' +convert (nvarchar), @count), 0);   else     insert into dbo.test004 values (' test_ ' +convert (nvarchar), @count), @count%4); end

More than one statement:

if (Boolen expression) begin    --statement 1    --Statement 2    --statement 3end  --Multiple statements are bound together into a single statement

Case: Basic syntax: Case<input expresstion> when <when expression> then <result expression>[...] [ELSE <result Expression>] END

SELECT  Id,name, [state]= case typeint%4 if 1 then ' delivery ' when 2 Then ' sign ' when 3 Then ' return ' ELSE '   unknown ' END from dbo. test004

While basic syntax: while <boolen expression>|<sql statement> [begin <statement Block>[break]<sql Statement>|<statement block>[continue] end];

Break is a forced end loop. CONTINUE back to the starting point and stop the current loop.

DECLARE @count int set @count =0;while (@count <100) begin set @[email protected]+1; INSERT into dbo.test004 values (' test_ ' +convert (nvarchar), @count), @count/4) End

WAITFOR: Basic syntax: waitfor delay< ' time ' > | Time < ' time ' >

The delay parameter specifies the period of time to wait and the number of days that can be specified-only hours, seconds, and the maximum allowable delay of 24 hours. Therefore, the following statement. WAITFOR DELAY ' 24:00 ' will run any code before the WAITFOR statement. The WAITFOR statement is then stopped for 24 hours before the next statement continues.

The time parameter parameter specifies the waiting period, cannot specify a date, can only be a 24-hour time, and the same maximum delay time is one day. Therefore, the following statement. WAITFOR time ' 00:00 ' will run any code before the WAITFOR statement until 0 o'clock in the morning to stop execution. After executing the WAITFOR statement, the next code is followed.

Conclusion: Because of limited knowledge, plus beginners, this chapter blog is not very good, there is time to revise. and re-write, there is a writing where the description is not clear ... Will be updated later ... The next chapter of the blog is the stored procedure.

SQL Getting Started Classic (vii) script and batch processing

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.