雏鹰部落

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1492|回复: 4

[转贴]自己整理的Transact_SQL语句

[复制链接]
发表于 2004-7-31 14:56:17 | 显示全部楼层 |阅读模式
[转贴]自己整理的Transact_SQL语句<br><br>*******************Transact_SQL********************<br>&#45;&#45;语 句                                功 能<br>&#45;&#45;数据操作<br>SELECT      &#45;&#45;从数据库表中检索数据行和列<br>INSERT      &#45;&#45;向数据库表添加新数据行<br>DELETE      &#45;&#45;从数据库表中删除数据行<br>UPDATE      &#45;&#45;更新数据库表中的数据<br>&#45;&#45;数据定义<br>CREATE TABLE    &#45;&#45;创建一个数据库表<br>DROP TABLE     &#45;&#45;从数据库中删除表<br>ALTER TABLE     &#45;&#45;修改数据库表结构<br>CREATE VIEW     &#45;&#45;创建一个视图<br>DROP VIEW     &#45;&#45;从数据库中删除视图<br>CREATE INDEX    &#45;&#45;为数据库表创建一个索引<br>DROP INDEX     &#45;&#45;从数据库中删除索引<br>CREATE PROCEDURE   &#45;&#45;创建一个存储过程<br>DROP PROCEDURE    &#45;&#45;从数据库中删除存储过程<br>CREATE TRIGGER    &#45;&#45;创建一个触发器<br>DROP TRIGGER    &#45;&#45;从数据库中删除触发器<br>CREATE SCHEMA    &#45;&#45;向数据库添加一个新模式<br>DROP SCHEMA     &#45;&#45;从数据库中删除一个模式<br>CREATE DOMAIN    &#45;&#45;创建一个数据值域<br>ALTER DOMAIN    &#45;&#45;改变域定义<br>DROP DOMAIN     &#45;&#45;从数据库中删除一个域<br>&#45;&#45;数据控制<br>GRANT      &#45;&#45;授予用户访问权限<br>DENY      &#45;&#45;拒绝用户访问<br>REVOKE      &#45;&#45;解除用户访问权限<br>&#45;&#45;事务控制<br>COMMIT      &#45;&#45;结束当前事务<br>ROLLBACK     &#45;&#45;中止当前事务<br>SET TRANSACTION    &#45;&#45;定义当前事务数据访问特征<br>&#45;&#45;程序化SQL<br>DECLARE      &#45;&#45;为查询设定游标<br>EXPLAN      &#45;&#45;为查询描述数据访问计划<br>OPEN      &#45;&#45;检索查询结果打开一个游标<br>FETCH      &#45;&#45;检索一行查询结果<br>CLOSE      &#45;&#45;关闭游标<br>PREPARE      &#45;&#45;为动态执行准备SQL 语句<br>EXECUTE      &#45;&#45;动态地执行SQL 语句<br>DESCRIBE     &#45;&#45;描述准备好的查询<br>&#45;&#45;-局部变量<br>declare @id char&#40;10&#41;<br>&#45;&#45;set @id = &#39;10010001&#39;<br>select @id = &#39;10010001&#39;<br>&#45;&#45;-全局变量<br>&#45;&#45;-必须以@@开头<br>&#45;&#45;IF ELSE<br>declare @x int @y int @z int<br>select @x = 1 @y = 2 @z=3<br>if @x &gt; @y<br>print &#39;x &gt; y&#39; &#45;&#45;打印字符串&#39;x &gt; y&#39;<br>else if @y &gt; @z<br>print &#39;y &gt; z&#39;<br>else print &#39;z &gt; y&#39;<br>&#45;&#45;CASE<br>use pangu<br>update employee<br>set e_wage =<br>case<br>when job_level = ’1’ then e_wage*1.08<br>when job_level = ’2’ then e_wage*1.07<br>when job_level = ’3’ then e_wage*1.06<br>else e_wage*1.05<br>end<br>&#45;&#45;WHILE CONTINUE BREAK<br>declare @x int @y int @c int<br>select @x = 1 @y=1<br>while @x &lt; 3<br>begin<br>print @x &#45;&#45;打印变量x 的值<br>while @y &lt; 3<br>  begin<br>   select @c = 100*@x + @y<br>   print @c &#45;&#45;打印变量c 的值<br>   select @y = @y + 1<br>  end<br>select @x = @x + 1<br>select @y = 1<br>end<br>&#45;&#45;WAITFOR<br>&#45;&#45;例 等待1 小时2 分零3 秒后才执行SELECT 语句<br>waitfor delay ’01:02:03’<br>select * from employee<br>&#45;&#45;例 等到晚上11 点零8 分后才执行SELECT 语句<br>waitfor time ’23:08:00’<br>select * from employee<br><br>***SELECT***<br>  select *&#40;列名&#41; from table_name&#40;表名&#41; where column_name operator value<br>  ex 宿主&#41;<br>select * from stock_information where stockid   = str&#40;nid&#41;<br>    stockname = &#39;str_name&#39; <br>    stockname like &#39;&#37; find this &#37;&#39; <br>    stockname like &#39;[a-zA-Z]&#37;&#39; &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;- &#40;[]指定值的范围&#41;<br>    stockname like &#39;[^F-M]&#37;&#39;   &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;- &#40;^排除指定范围&#41;<br>    &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;- 只能在使用like关键字的where子句中使用通配符&#41;<br>    or stockpath = &#39;stock_path&#39;<br>    or stocknumber &lt; 1000<br>    and stockindex = 24<br>    not stocksex = &#39;man&#39;<br>    stocknumber between 20 and 100<br>    stocknumber in&#40;10,20,30&#41;<br>    order by stockid desc&#40;asc&#41; &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;- 排序,desc-降序,asc-升序<br>    order by 1,2 &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;- by列号<br>    stockname = &#40;select stockname from stock_information  where stockid  = 4&#41;<br>    &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;- 子查询<br>    &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;- 除非能确保内层select只返回一个行的值,<br>    &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;- 否则应在外层where子句中用一个in限定符<br>select distinct column_name form table_name &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;- distinct指定检索独有的列值,不重复<br>select stocknumber ,&quot;stocknumber + 10&quot; = stocknumber + 10 from table_name<br>select stockname , &quot;stocknumber&quot; = count&#40;*&#41; from table_name group by stockname<br>                                     &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;- group by 将表按行分组,指定列中有相同的值<br>         having count&#40;*&#41; = 2  &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;-  having选定指定的组<br>       <br>select * <br>from table1, table2                  <br>where table1.id *= table2.id &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45; 左外部连接,table1中有的而table2中没有得以null表示<br>    table1.id =* table2.id &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45; 右外部连接 <br>select stockname from table1<br>union [all]  &#45;&#45;&#45;&#45;-  union合并查询结果集,all-保留重复行<br>select stockname from table2<br>***insert***<br>insert into table_name &#40;Stock_name,Stock_number&#41; value &#40;&quot;xxx&quot;,&quot;xxxx&quot; <br>             value &#40;select Stockname , Stocknumber from Stock_table2&#41;&#45;&#45;-value为select语句<br>***update***<br>update table_name set Stockname = &quot;xxx&quot; [where Stockid = 3]<br>        Stockname = default<br>        Stockname = null<br>        Stocknumber = Stockname + 4<br>***delete***<br>delete from table_name where Stockid = 3<br>truncate table_name &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;- 删除表中所有行,仍保持表的完整性<br>drop table table_name &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;- 完全删除表<br>***alter table*** &#45;&#45;- 修改数据库表结构<br>alter table database.owner.table_name add column_name char&#40;2&#41; null .....<br>sp_help table_name &#45;&#45;&#45;&#45; 显示表已有特征<br>create table table_name &#40;name char&#40;20&#41;, age smallint, lname varchar&#40;30&#41;&#41;<br>insert into table_name select ......... &#45;&#45;&#45;&#45;- 实现删除列的方法(创建新表)<br>alter table table_name drop constraint Stockname_default &#45;&#45;&#45;&#45; 删除Stockname的default约束<br>   <br>***function&#40;/*常用函数*/&#41;***<br>&#45;&#45;&#45;&#45;统计函数&#45;&#45;&#45;&#45;<br>AVG    &#45;&#45;求平均值<br>COUNT   &#45;&#45;统计数目<br>MAX    &#45;&#45;求最大值<br>MIN    &#45;&#45;求最小值<br>SUM    &#45;&#45;求和<br>&#45;&#45;AVG<br>use pangu<br>select avg&#40;e_wage&#41; as dept_avgWage<br>from employee<br>group by dept_id<br>&#45;&#45;MAX<br>&#45;&#45;求工资最高的员工姓名<br>use pangu<br>select e_name<br>from employee<br>where e_wage =<br>&#40;select max&#40;e_wage&#41;<br>from employee&#41;<br>&#45;&#45;STDEV&#40;&#41;<br>&#45;&#45;STDEV&#40;&#41;函数返回表达式中所有数据的标准差<br>&#45;&#45;STDEVP&#40;&#41;<br>&#45;&#45;STDEVP&#40;&#41;函数返回总体标准差<br>&#45;&#45;VAR&#40;&#41;<br>&#45;&#45;VAR&#40;&#41;函数返回表达式中所有值的统计变异数<br>&#45;&#45;VARP&#40;&#41;<br>&#45;&#45;VARP&#40;&#41;函数返回总体变异数<br>&#45;&#45;&#45;&#45;算术函数&#45;&#45;&#45;&#45;<br>/***三角函数***/<br>SIN&#40;float_expression&#41; &#45;&#45;返回以弧度表示的角的正弦<br>COS&#40;float_expression&#41; &#45;&#45;返回以弧度表示的角的余弦<br>TAN&#40;float_expression&#41; &#45;&#45;返回以弧度表示的角的正切<br>COT&#40;float_expression&#41; &#45;&#45;返回以弧度表示的角的余切<br>/***反三角函数***/<br>ASIN&#40;float_expression&#41; &#45;&#45;返回正弦是FLOAT 值的以弧度表示的角<br>ACOS&#40;float_expression&#41; &#45;&#45;返回余弦是FLOAT 值的以弧度表示的角<br>ATAN&#40;float_expression&#41; &#45;&#45;返回正切是FLOAT 值的以弧度表示的角<br>ATAN2&#40;float_expression1,float_expression2&#41; <br>       &#45;&#45;返回正切是float_expression1 /float_expres-sion2的以弧度表示的角<br>DEGREES&#40;numeric_expression&#41;<br>                      &#45;&#45;把弧度转换为角度返回与表达式相同的数据类型可为<br>       &#45;&#45;INTEGER/MONEY/REAL/FLOAT 类型<br>RADIANS&#40;numeric_expression&#41; &#45;&#45;把角度转换为弧度返回与表达式相同的数据类型可为<br>       &#45;&#45;INTEGER/MONEY/REAL/FLOAT 类型<br>EXP&#40;float_expression&#41;  &#45;&#45;返回表达式的指数值<br>LOG&#40;float_expression&#41;  &#45;&#45;返回表达式的自然对数值<br>LOG10&#40;float_expression&#41;&#45;&#45;返回表达式的以10 为底的对数值<br>SQRT&#40;float_expression&#41; &#45;&#45;返回表达式的平方根<br>/***取近似值函数***/<br>CEILING&#40;numeric_expression&#41;  &#45;&#45;返回&gt;=表达式的最小整数返回的数据类型与表达式相同可为<br>       &#45;&#45;INTEGER/MONEY/REAL/FLOAT 类型<br>FLOOR&#40;numeric_expression&#41;    &#45;&#45;返回&lt;=表达式的最小整数返回的数据类型与表达式相同可为<br>       &#45;&#45;INTEGER/MONEY/REAL/FLOAT 类型<br>ROUND&#40;numeric_expression&#41;    &#45;&#45;返回以integer_expression 为精度的四舍五入值返回的数据<br>       &#45;&#45;类型与表达式相同可为INTEGER/MONEY/REAL/FLOAT 类型<br>ABS&#40;numeric_expression&#41;      &#45;&#45;返回表达式的绝对值返回的数据类型与表达式相同可为<br>       &#45;&#45;INTEGER/MONEY/REAL/FLOAT 类型<br>SIGN&#40;numeric_expression&#41;     &#45;&#45;测试参数的正负号返回0 零值1 正数或-1 负数返回的数据类型<br>       &#45;&#45;与表达式相同可为INTEGER/MONEY/REAL/FLOAT 类型<br>PI&#40;&#41;       &#45;&#45;返回值为π 即3.1415926535897936<br>RAND&#40;[integer_expression]&#41;   &#45;&#45;用任选的[integer_expression]做种子值得出0-1 间的随机浮点数<br><br>&#45;&#45;&#45;&#45;字符串函数&#45;&#45;&#45;&#45;<br>ASCII&#40;&#41;         &#45;&#45;函数返回字符表达式最左端字符的ASCII 码值<br>CHAR&#40;&#41;   &#45;&#45;函数用于将ASCII 码转换为字符<br>   &#45;&#45;如果没有输入0 ~ 255 之间的ASCII 码值CHAR 函数会返回一个NULL 值<br>LOWER&#40;&#41;   &#45;&#45;函数把字符串全部转换为小写<br>UPPER&#40;&#41;   &#45;&#45;函数把字符串全部转换为大写<br>STR&#40;&#41;   &#45;&#45;函数把数值型数据转换为字符型数据<br>LTRIM&#40;&#41;   &#45;&#45;函数把字符串头部的空格去掉<br>RTRIM&#40;&#41;   &#45;&#45;函数把字符串尾部的空格去掉<br>LEFT&#40;&#41;,RIGHT&#40;&#41;,SUBSTRING&#40;&#41;  &#45;&#45;函数返回部分字符串<br>CHARINDEX&#40;&#41;,PATINDEX&#40;&#41;  &#45;&#45;函数返回字符串中某个指定的子串出现的开始位置<br>SOUNDEX&#40;&#41;  &#45;&#45;函数返回一个四位字符码 <br>   &#45;&#45;SOUNDEX函数可用来查找声音相似的字符串但SOUNDEX函数对数字和汉字均只返回0 值     <br>DIFFERENCE&#40;&#41;    &#45;&#45;函数返回由SOUNDEX 函数返回的两个字符表达式的值的差异<br>   &#45;&#45;0 两个SOUNDEX 函数返回值的第一个字符不同<br>   &#45;&#45;1 两个SOUNDEX 函数返回值的第一个字符相同<br>   &#45;&#45;2 两个SOUNDEX 函数返回值的第一二个字符相同<br>   &#45;&#45;3 两个SOUNDEX 函数返回值的第一二三个字符相同<br>   &#45;&#45;4 两个SOUNDEX 函数返回值完全相同<br>                                      <br>QUOTENAME&#40;&#41;  &#45;&#45;函数返回被特定字符括起来的字符串<br>/*select quotename&#40;&#39;abc&#39;, &#39;{&#39;&#41; quotename&#40;&#39;abc&#39;&#41;<br>运行结果如下<br>&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;{<br>{abc} [abc]*/<br>REPLICATE&#40;&#41;     &#45;&#45;函数返回一个重复character_expression 指定次数的字符串<br>/*select replicate&#40;&#39;abc&#39;, 3&#41; replicate&#40; &#39;abc&#39;, -2&#41;<br>运行结果如下<br>&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;- &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;-<br>abcabcabc NULL*/<br>REVERSE&#40;&#41;       &#45;&#45;函数将指定的字符串的字符排列顺序颠倒<br>REPLACE&#40;&#41;       &#45;&#45;函数返回被替换了指定子串的字符串<br>/*select replace&#40;&#39;abc123g&#39;, &#39;123&#39;, &#39;def&#39;&#41;<br>运行结果如下<br>&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;- &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;-<br>abcdefg*/<br>SPACE&#40;&#41;   &#45;&#45;函数返回一个有指定长度的空白字符串<br>STUFF&#40;&#41;   &#45;&#45;函数用另一子串替换字符串指定位置长度的子串<br><br>&#45;&#45;&#45;&#45;数据类型转换函数&#45;&#45;&#45;&#45;<br>CAST&#40;&#41; 函数语法如下<br>CAST&#40;&#41; &#40;&lt;expression&gt; AS &lt;data_ type&gt;[ length ]&#41;<br>CONVERT&#40;&#41; 函数语法如下<br>CONVERT&#40;&#41; &#40;&lt;data_ type&gt;[ length ], &lt;expression&gt; [, style]&#41;<br>select cast&#40;100+99 as char&#41; convert&#40;varchar&#40;12&#41;, getdate&#40;&#41;&#41;<br>运行结果如下<br>&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45; &#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;<br>199   Jan 15 2000<br>&#45;&#45;&#45;&#45;日期函数&#45;&#45;&#45;&#45;<br>DAY&#40;&#41;   &#45;&#45;函数返回date_expression 中的日期值<br>MONTH&#40;&#41;   &#45;&#45;函数返回date_expression 中的月份值<br>YEAR&#40;&#41;   &#45;&#45;函数返回date_expression 中的年份值<br>DATEADD&#40;&lt;datepart&gt; ,&lt;number&gt; ,&lt;date&gt;  <br>   &#45;&#45;函数返回指定日期date 加上指定的额外日期间隔number 产生的新日期<br>DATEDIFF&#40;&lt;datepart&gt; ,&lt;number&gt; ,&lt;date&gt; <br>   &#45;&#45;函数返回两个指定日期在datepart 方面的不同之处<br>DATENAME&#40;&lt;datepart&gt; , &lt;date&gt;   &#45;&#45;函数以字符串的形式返回日期的指定部分<br>DATEPART&#40;&lt;datepart&gt; , &lt;date&gt;   &#45;&#45;函数以整数值的形式返回日期的指定部分<br>GETDATE&#40;&#41;  &#45;&#45;函数以DATETIME 的缺省格式返回系统当前的日期和时间<br>&#45;&#45;&#45;&#45;系统函数&#45;&#45;&#45;&#45;<br>APP_NAME&#40;&#41;      &#45;&#45;函数返回当前执行的应用程序的名称<br>COALESCE&#40;&#41;  &#45;&#45;函数返回众多表达式中第一个非NULL 表达式的值<br>COL_LENGTH&#40;&lt;&#39;table_name&#39;&gt;, &lt;&#39;column_name&#39;&gt;  &#45;&#45;函数返回表中指定字段的长度值<br>COL_NAME&#40;&lt;table_id&gt;, &lt;column_id&gt;    &#45;&#45;函数返回表中指定字段的名称即列名<br>DATALENGTH&#40;&#41; &#45;&#45;函数返回数据表达式的数据的实际长度<br>DB_ID&#40;[&#39;database_name&#39;]&#41; &#45;&#45;函数返回数据库的编号<br>DB_NAME&#40;database_id&#41;  &#45;&#45;函数返回数据库的名称<br>HOST_ID&#40;&#41;     &#45;&#45;函数返回服务器端计算机的名称<br>HOST_NAME&#40;&#41;     &#45;&#45;函数返回服务器端计算机的名称<br>IDENTITY&#40;&lt;data_type&gt;[, seed increment]&#41; [AS column_name]&#41;<br>&#45;&#45;IDENTITY&#40;&#41; 函数只在SELECT INTO 语句中使用用于插入一个identity column列到新表中<br>/*select identity&#40;int, 1, 1&#41; as column_name<br>into newtable<br>from oldtable*/<br>ISDATE&#40;&#41;  &#45;&#45;函数判断所给定的表达式是否为合理日期<br>ISNULL&#40;&lt;check_expression&gt;, &lt;replacement_value&gt;  &#45;&#45;函数将表达式中的NULL 值用指定值替换<br>ISNUMERIC&#40;&#41;  &#45;&#45;函数判断所给定的表达式是否为合理的数值<br>NEWID&#40;&#41;   &#45;&#45;函数返回一个UNIQUEIDENTIFIER 类型的数值<br>NULLIF&#40;&lt;expression1&gt;, &lt;expression2&gt; <br>&#45;&#45;NULLIF 函数在expression1 与expression2 相等时返回NULL 值若不相等时则返回expression1 的值<br>

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
 楼主| 发表于 2004-7-31 14:57:04 | 显示全部楼层
可以当工具书查一查...<br>希望对大家有帮助...<br><br>[em1]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
 楼主| 发表于 2004-8-1 13:44:15 | 显示全部楼层
都没有人顶!!!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
发表于 2004-8-1 15:05:20 | 显示全部楼层
8错,我这样的懒鬼最喜欢这个了,顶一个![em4]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
发表于 2004-8-1 15:48:16 | 显示全部楼层
不错,这些都能消化得了就是高手了。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|熊猫同学技术论坛|小黑屋| 网络工程师论坛 ( 沪ICP备09076391 )

GMT+8, 2024-11-22 13:52 , Processed in 0.086938 second(s), 18 queries , Gzip On.

快速回复 返回顶部 返回列表