首页 > 数据库 >Transact-SQL (T-SQL)

Transact-SQL (T-SQL)

时间:2023-07-03 16:36:45浏览次数:36  
标签:BEGIN END Transact Server SQL PRINT


 http://en.wikipedia.org/wiki/Transact-SQL

Transact-SQL


From Wikipedia, the free encyclopedia




Jump to: navigation, search



Transact-SQL (T-SQL) is Microsoft's and Sybase's proprietary extension to SQL. SQL, often expanded to Structured Query Language, is a standardized computer language that was originally developed by IBM for querying, altering and defining relational databases, using declarative statements. T-SQL expands on the SQL standard to include procedural programming, local variables, various support functions for string processing, date processing, mathematics, etc. and changes to the DELETE and UPDATE statements. These additional features make Transact-SQL Turing complete.

Transact-SQL is central to using Microsoft SQL Server. All applications that communicate with an instance of SQL Server do so by sending Transact-SQL statements to the server, regardless of the user interface of the application.


Contents


 [hide] 




  • 1 Flow control
  • 2 Changes to DELETE and UPDATE statements
  • 3 BULK INSERT
  • 4 TRY CATCH
  • 5 See also
  • 6 External links

[edit] Flow control

Keywords for flow control in Transact-SQL include BEGIN and END, BREAK, CONTINUE, GOTO, IF and ELSE, RETURN, WAITFOR, and WHILE.

IF and ELSE allow conditional execution. This batch statement will print "It is the weekend" if the current date is a weekend day, or "It is a weekday" if the current date is a weekday.



IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1
   PRINT 'It is the weekend.'
ELSE
   PRINT 'It is a weekday.'



BEGIN and END mark a block of statements. If more than one statement is to be controlled by the conditional in the example above, we can use BEGIN and END like this:



IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1
BEGIN
   PRINT 'It is the weekend.'
   PRINT 'Get some rest on the weekend!'
END
ELSE
BEGIN
   PRINT 'It is a weekday.'
   PRINT 'Get to work on a weekday!'
END



WAITFOR will wait for a given amount of time, or until a particular time of day. The statement can be used for delays or to block execution until the set time.

RETURN is used to immediately return from a stored procedure or function.

BREAK ends the enclosing WHILE loop, while CONTINUE causes the next iteration of the loop to execute. An example of a WHILE loop is given below.



DECLARE @i INT
SET @i = 0
 
WHILE @i < 5
BEGIN
   PRINT 'Hello world.'
   SET @i = @i + 1
END



[edit] Changes to DELETE and UPDATE statements

In Transact-SQL, both the DELETE and UPDATE statements allow a FROM clause to be added, which allows joins to be included.

This example deletes all users who have been flagged with the 'Idle' flag.



DELETE users 
  FROM users as u
  INNER JOIN user_flags as f
    ON u.id=f.id
 WHERE f.name = 'Idle'



[edit] BULK INSERT

BULK INSERT is a Transact-SQL statement that implements a bulk data-loading process, inserting multiple rows into a table, reading data from an external sequential file. Use of BULK INSERT results in better performance than processes that issue individual INSERT statements for each row to be added. Additional details are available on Microsoft's MSDN page.

[edit] TRY CATCH

Beginning with SQL Server 2005, Microsoft introduced additional TRY CATCH logic to support exception type behaviour. This behaviour enables developers to simplify their code and leave out @@ERROR checking after each SQL execution statement.



-- begin transaction
BEGIN TRAN
 
BEGIN TRY
   -- execute each statement
   INSERT INTO MYTABLE(NAME) VALUES ('ABC')
   INSERT INTO MYTABLE(NAME) VALUES ('123')
 
   -- commit the transaction
   COMMIT TRAN
END TRY
BEGIN CATCH
   -- rollback the transaction because of error
   ROLLBACK TRAN
END CATCH



[edit] See also

  • Adaptive Server Enterprise (Sybase)
  • PL/SQL (Oracle)
  • PL/pgSQL (PostgreSQL)

[edit] External links


 

标签:BEGIN,END,Transact,Server,SQL,PRINT
From: https://blog.51cto.com/u_16174476/6612640

相关文章

  • MYSQL数据库转DM达梦数据库函数替换及注意事项
    1、调整IF函数为 case 函数MYSQL: IF(condition, value_if_true, value_if_false) if(a.class_sort_code='0301',(selectgroup_concat(sku_attr_id)sku_Attrfroma_sku_attr_relaWHEREmodel_id=a.model_idorderbysku_attr_id),'')sku_attrD......
  • pgsql 搭建
    1.yum安装yuminstall-yhttps://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpmyuminstall-ypostgresql12-server2.启动pgsql/usr/pgsql-12/bin/postgresql-12-setupinitdbsystemctlenablepostgresql-12system......
  • mysql 配置主从复制
    推荐编译安装,但是太麻烦了,所以直接docker安装。参考https://blog.csdn.net/abcde123_123/article/details/106244181https://www.cnblogs.com/songwenjie/p/9371422.html拉取镜像推荐使用mysql5.7dockerpullmysql:5.7.39启动两个服务https://zhuanlan.zhihu.com/......
  • MySQL数据迁移
    前言在进行迁移时,源mysql的配置和目标mysql的配置应尽量保持一致迁移所有数据库迁移前,源端有以下数据库:迁移前,目标端有以下数据库目标端是刚安装好的mysql,默认就有上图中的4个库,源端比目标端多了一个dan库在源端备份所有数据库[root@target_pcdatabasefile]# mysql......
  • Mysql基础篇(四)之事务
    一.事务简介事务是一组操作的集合,它是一个不可分隔的工作单位,事务会把所有的操作作为一个整体一起向系统提交或撤销操作请求,即这些操作要么同时成功,要么同时失败。就比如:张三给李四转账1000块钱,张三银行账户的钱减少了1000,而李四银行账户的钱要增加1000。这一组操作就必须在一......
  • SQL
    增insertintotable(字段名1,字段2)values(值1,值2);改update表名set字段1=值1,字段2=值2【where条件】;删deletefrom表名[where条件];查select字段from表名where条件列表groupby分组字段列表having分组后的条件列表orderby排序字段列表limit分页参数去除重复记录select......
  • 完全兼容DynamoDB协议!GaussDB(for Cassandra)为NoSQL注入新活力
    摘要:DynamoDB是一款托管式的NoSQL数据库服务,支持多种数据模型,广泛应用于电商、社交媒体、游戏、IoT等场景。本文分享自华为云社区《完全兼容DynamoDB协议!GaussDB(forCassandra)为NoSQL注入新活力》,作者:GaussDB数据库。DynamoDB是一款托管式的NoSQL数据库服务,支持多种数据模型......
  • Flask SQLALCHEMY Model 模型
    FlaskSQLALCHEMYModel模型classPerson(db.Model): __tablename__='person' id=db.Column(db.Integer,primary_key=True) name=db.Column(db.string(16),unique=True) def__repr__(self):return'<User{}>'.format(self......
  • SQL Server中的NULL值处理:判断与解决方案
    摘要:在SQLServer数据库中,NULL是表示缺少数据或未知值的特殊标记。处理NULL值是SQL开发人员经常遇到的问题之一。本文将介绍SQLServer中判断和处理NULL值的不同方法,以及一些解决方案,帮助您更好地处理数据库中的NULL值情况。文章内容:引言:在数据库开发中,经常会遇到处理......
  • 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in
    项目场景:mysql创建function报错误1418-ThisfunctionhasnoneofDETERMINISTIC,NOSQL,orREADSSQLDATAin问题描述:执行创建函数的sql语句时,提示:ThisfunctionhasnoneofDETERMINISTIC,NOSQL,orREADSSQLDATAinitsdeclarationandbinaryloggingisenab......