TRANSACT

zhaozj2021-02-16  46

TRANSACT_SQL Little Manual Wtadminxjeri [Original]

Original location: http://dev.9cbs.net/Article/29/29381.shtm

- Statement function - Data operation select - Retrieves Dark Data from Database Table and Column Insert - Add new data line to Database table DELETE - Remove Data Row Update from Database Table - Update Data in Database Table - - Data Definition Create Table - Create a Database Table Drop Table - Remove Table ALTER TABLE from the Database - Modify Database Table Structure Create View - Create a View Drop View - Remove View Create Index from the Database - for Database Table Create a Index Drop Index - Remove Index Create Procedure from the Database - Create a Stored Procedure Drop Procedure - Remove Storage Procedure from the Database Create Trigger - Create a trigger DRIGGER - Remove the trigger Create from the database Schema - Add a new mode to the database Drop Schema - Remove a mode from the database Create Domain - Create a Data Value ALTER DOMAIN - Change the Domain Definition DOMAIN - Delete a domain from the database - Data Control Grant - Grant user access deny - Reject user access REVOKE - Release user access - Transaction control commit - End current transaction Rollback - Statue Current Transaction SET Transaction - Define Current Services Data Access Features - Programming Sqldeclare - Set the cursor explan for the query Description Data Access Plan Open - Retrieval Query Result Open a Camper Fetch - Retrieve a line Query Results Close - Close the Cursor Prepare - Prepare SQL Statements Execute - Dynamic Execute SQL statement Describe - Description Prepared query

--- Local variable declare @id char (10) - set @ID = '10010001'select @ID =' 10010001 '

--- Global variable - must start with @@

- IF Elsedeclare @x int @y int @z @ @X = 1 @Y = 2 @ z = 3if @x> @YPRINT 'X> Y' - Print string 'x> Y'Else if @Y> @zprint 'y> Z'Else Print' Z> Y '

--Caseuset e_wage = Casewhen Job_LAGE = '1' TEN E_WAGE * 1.08When Job_WAGE * 1.08When Job_LEVEL = '2' TEN E_WAGE * 1.07When Job_LEVEL = '3' TEN E_WAGE * 1.06ELSE E_WAGE * 1.05nd

--While Continue BreakDeclare @x int @x = 1 @ y = 1WHILE @X <3beginprint @X - the value of print variable x While @Y <3begInselectr @c = 100 * @ x @Yprint @c - Value of the Cable Cost C SELECT @Y = @Y 1ndSelect @X = @X 1Select @Y = 1END - Waitfor - Example Waiting for 1 hour 2 points after 3 seconds to execute SELECT statement waitfor delay ' 01: 02: 03'select * from Employee - Examples waiting until 11:00 in the evening to perform SELECT statement waitfor Time '23: 08: 00'Select * from Employee

*** SELECT ***

select * (column names) from table_name (table) where column_name operator valueex host) select * from stock_information where stockid = str (nid) stockname = 'str_name' stockname like '% find this%' stockname like '[a-zA- Z]% '----------------- (^ exclude specified range) ------- (^ exclude specified range) ----- ---- You can only use wildcards in WHERE clauses using the LIKE key) or stockpath = 'stock_path'or stocknumber <1000and stockIndex = 24Not Stocksex =' Man'StockNumber Between 20 and 100stockNumber in (10, 20, 30) Order by stockid desc (ASC) --------- Sort, Desc - Descending, ASC-Ascending Order By 1, 2 --------- BY list stockName = (Select stockname from stock_information Where Stockid = 4) -------- subquery --------- Unless you can make sure that the inner SELECT returns only a row of values, -------, otherwise it should be outside where clause with a qualifier in select distinct column_name form table_name --------- distinct specified column values ​​retrieved unique, not repeated select stocknumber, "stocknumber 10" = stocknumber 10 from table_nameselect stockname, "stocknumber" = count (*) from table_name group by stockName --------- Group by Packets the table, specify the same value in the specified column HAVING Count (*) = 2 ------ --- Having Selected Group

Select * from table1, table2 where table1.id * = Table2.ID ------- The left external connection, there is a Table1 in Table1 to indicate Table1.ID = * Table2.ID ---- ---- Right Exterior Select stockname from table1Union [all] ----- UNION merge query result set, all-reserved repeat line Select stockname from table2

*** insert ***

INSERT INTO TABLE_NAME (STOCK_NAME, STOCK_NUMBER) Value ("XXX", "XXXXX") Value (SELECT stockname, stock_table2) --- value is a SELECT statement

*** UPDATE ***

Update table_name set stockname = "xxx" [Where stockid = 3] stockname = defaultStockName = nullstocknumber = stockname 4

*** Delete ***

Delete from table_name where stockid = 3truncate table_name ----------- Delete all rows in the table, still keep the integrity of the table DROP TABLE_NAME ------------- complete Delete table

*** alter Table *** --- Modify the database table structure

Alter Table Database.OWNER.TABLE_NAME ADD Column_name Char (2) NULL ..... sp_help table_name ---- Display Table Existing Create Table Table_name (Name Char (20), Age Smallint, Lname Varchar (30)) INSERT INTO TABLE_NAME SELECT ......... --- Implement the method of deleting columns (Create a new table) ALTER TABLE TABLE_NAME DROP CONSTRAINT stockName_DEFAULT ---- Delete Stockname's default constraint

*** Function (/ * Common function * /) ***

---- Statistical Function ---- AVG - Ask the average COUNT - Statistics Max - Search MIN - Summary SUM - Search

--AVGUSE PANGUSELECT AVG (E_WAGE) AS DEPT_AVGWAGEFROM EMPLOYEGROUP BY DEPT_ID

--MAX - Survey the highest payroll Name USE PANGUSELECT E_NAMEFROM EMPLOYEEWHERE E_WAGE = (SELECT MAX (E_WAGE) from EMPLOYEE)

--Stdev () - stdev () function Returns the standard deviation of all data in the expression

--Stdevp () - stdevp () function returns the overall standard difference

--VAR () - VAR () function returns a statistical variation of all values ​​in the expression

--VARP () - VARP () function Returns the total variation

---- Arithmetic function ----

/ *** Triangle function *** / sin (float_expression) - Returns sinusoidal COS (FLOAT_EXPRESSION) in an arc - Returns the cosine TAN (FLOAT_EXPIXPRESSION) in the arc-represented angle - Returns the angle represented by radians Orthodium COT (FLOAT_EXPRESSION) - Returns the remainder of the angle represented / *** Anti-triangular function *** / asin (float_expression) - Returns the sinusoidal is the angle ACOS (float_expression) indicated by the FLOAT value. - Returning the cosine is the angle ATAN (float_expression) of the float value - Return to the angle ATAN2 (float_expression1, float_expression2) of the float value - Return to orthode chitting is FLOAT_EXPRESSION1 / FLOAT_EXPRES-SION2 in the arc. Corner DegRees - Transform the arc into angle returns the same data type as the expression can be the same as the integer / money / real / float type radians (numeric_expression) - convert the angle to the arc return to the same data as the expression Types can be -integer / money / real / float type exp (float_expression) - Return Expression LOG (FLOAT_EXPRESSION) - Returns the natural log of log10 (float_expression) - Returns 10 For the end of the logarian SQRT (FLOAT_EXPRESSION) - Returns the square root of the expression / *** Nearly close to the similar value *** / ceiling (numeric_expression) - Return> = The minimum integer returned by the expression is the same as the expression To - ITEGER / MONEY / REAL / FLOAT Type Floor (Numeric_Expression) - Returns the data type returned by <= the minimum integer returned by the expression is the same as the expression of the same --integer / money / real / float type Round (numeric_expression) - - Returns the data returned by integer_expression as accuracy - Types and expressions can be integer / money / real / float type ABS (Nume) RIC_EXPRESSION) - The data type returned by the error of the expression can be the same as the expression of the expression of -integer / money / real / float type Sign (Numeric_Expression) - the positive number of the test parameters returns 0 zero value 1 positive or -1 negative data type - the same can be used for Integer / Money / REAL / FLOAT type PI () - return value is π 3.1415926535897936rand ([Integer_Expression]) - Do it with optional [integer_expression] Seeds worth 0-1 random floating point numbers

---- String Function ---- ASCII () - Function Returns the ASCII code value of the character express left-end character Char () - Function is used to convert the ASCII code to the character - if not entered 0 ~ 255 The ASCII code value char function returns a NULL value LOWER () - function transforms all the strings to lowercase UPPER () - Function Converts the string to the uppercase STR () -Function to convert numeric data conversion For the character data Ltrim () - function removes the space of the string head removes the RTRIM () - function removes the space of the string of the string to Left (), substring (), substring () - Function Return to some strings Charindex (), Patindex () - Function Returns the start position of a specified substring in the string Soundex () - function Returns a four-bit character code - Soundex function can be used to find sound similar strings, but Soundex functions Return to numbers and Chinese characters to return 0 Values ​​DIFFERENCE () - Functions Returns the difference between the two character expressions returned by the SoundEx function - 0 two SoundEx functions return value different - 1 two SoundEx function returns the first character of the value of the same - 2 Two SoundEx functions return values ​​The first two characters of the value of the same - 3 Two SoundEx functions return values ​​The first two three characters of the value - 4 two SoundEx functions The return value is completely the same quotename () - function returns a string from a specific pen / * select quotename ('abc', '{') quotename ('ABC') running results --------- ------------------------- {{abc} [ABC] * /

Replicate () - Function Returns a string of repeating character_expression specified / * SELECT Replicate ('ABC', 3) Replicate ('ABC', -2) Run the results below ----------- ---------- Abcabcabc null * /

Reverse () - Function Run the specified string Character arrangement reverse the Replace () - function returns the string of the specified substring / * SELECT Replace ('ABC123G', '123', 'DEF') The results are as follows ----------- ----------- Abcdefg * /

The space () - function returns a blank string stuff () - function with a specified length, replacing a string of the position length with another subster

---- Data Type Conversion Function ---- Cast () Function Syntax As follows Cast () ()

AS

[Length])

Convert () function syntax is as follows

CONVERT () ()

[Length],

STYLE])

SELECT CAST (100 99 As Char) Convert (VARCHAR (12), getdate ()) running results --------------------------- --- ------------ 199 Jan 15 2000

---- Date Function ---- DAY () - Function Returns the Date_Expression Date () - Function Returns the month value of Date_EXPRESSION YEAR () - Function Returns Date_EXPRESSION Dateadd (

,

,

)

- Function Returns the New Date Generated by Number Generated by Specified Date Date plus Number

Datediff

,

,

)

- Function Returns two specified dates from DATEPART

Datename

,

) - Function Returns the specified section of the date in the form of a string

DatePart

,

) - Function Returns the specified section of the date in the form of an integer value

GetDate () - Function Returns the current date and time of the system at the default format of DateTime

---- System Function ---- App_Name () - Function Returns the name of the currently executed application Coalesce () - Function Returns the value of the first non-NULL expression in many expressions Col_length (, ) - Function Returns the length value col_name of the specified field in the table (,

) - Function Return to the name of the specified field in the table is listed

DATALEENGTH () - function returns the actual length of data of data expressions

DB_ID (['Database_name'] - Function Returns the number of the database

DB_NAME (Database_ID) - Function Returns Name of Database

Host_ID () - Function Returns the name of the server computer computer

Host_name () - Function Returns the name of the server computer computer

Identity

[SEED Increment]) [as column_name])

--IDENTITY () function is only used in the SELECT INTO statement to insert an Identity Column to list a new table

/ * SELECT IDENTITY (Int, 1, 1) as column_name

INTO NewTable

From oldtable * /

Isdate () - function judgment is a reasonable date for a given expression

Isnull

,

) - The function replaces the NULL value in the expression.

ISNUMERIC () - Function determines if the given expression is a reasonable value

NewID () - function returns a NE value of a UniqueIdentifier type

Nullif

,

)

- Nullif Function Returns the value of Expression1 when Expression1 is equal to Expression2 equal to EXPRESSION2.

转载请注明原文地址:https://www.9cbs.com/read-16952.html

New Post(0)