/*基本查询*/ use db_sql2000 GO SELECT * FROM Books --定义批处Begin ... END USE db_sql2000 GO --声明变量 DECLARE @name varchar(200),@price money,@introduction varchar(200) SELECT @name=b_name,@price=b_price,@introduction=b_intro FROM dbo.Bookinfo2 WHERE b_name='SQL深入编程' --根究条件输出 IF @price>20 BEGIN PRINT '书名 :'+@name PRINT '内容介绍: '+@introduction END ELSE PRINT '此书价格小于20' GO SELECT * FROM dbo.Bookinfo2 DECLARE @book_name varchar(200),@book_price money SELECT @book_name = b_name,@book_price = b_price FROM dbo.Bookinfo2 WHERE b_name = 'SQL深入编程' IF @book_price>20 BEGIN PRINT ' 书名: '+@book_name ; --PRINT '价格: '+CAST(@book_price AS varchar(200)) ;/*money类型不能自动转换为字符串类型*/ PRINT '价格: '+CONVERT(varchar(200),@book_price) ; END ELSE PRINT '此书价格少于20' GO --查询指定行数据TOP SELECT TOP 3 * FROM dbo.Bookinfo2 WHERE dbo.Bookinfo2.b_price>10 ORDER BY b_price GO --计算数据在结果集中的行号ROW_NUMBER() SELECT * FROM Goods SELECT ROW_NUMBER() OVER(ORDER BY g_date) AS 行号, g_name AS '商品名', g_lprice AS '会员价', g_provider AS '提供商', g_date AS '出厂日期' FROM Goods --NWEID():创建uniqueidentifier类型的惟一值 DECLARE @var uniqueidentifier SET @var = NEWID() --PRINT '@var变量的值为:'+CAST(@var AS varchar(200)) PRINT '@var变量的值为:'+convert(varchar(200),@var) --查询指定长度的文本类型数据 --READTEXT:用于读取text、ntext或image列中的数据 --TEXTPTR()函数:用于返回对应于text、ntext或image列的文本指针 --SET TEXTSIZE:指定由SELECT语句返回的text和ntext数据的大小 SELECT * FROM Bookinfo DECLARE @temp varbinary(16) SELECT @temp = TEXTPTR(isnull(b_intro,'')) FROM Bookinfo AS b INNER JOIN Sales AS s ON b.b_code = s.book_code AND sal_tot = (SELECT MAX(sal_tot) FROM Sales) ORDER BY sal_tot DESC READTEXT Bookinfo.b_intro @temp 0 10 GO --格式化数据集 --CONVERT():将某种数据类型的表达式显示转换为另一种数据类型 DECLARE @vartime DATETIME SET @vartime = '2012-11-18' PRINT '日期: '+CONVERT(VARCHAR(200),@vartime,101) --CAST():和CONVERT()作用相同 DECLARE @vartime DATETIME SET @vartime = '2012-11-18' PRINT '日期: '+CAST(@vartime as VARCHAR) --SubString(字符表达式,start,length)返回数据的一部分 DECLARE @varTemp varchar(200) SET @varTemp = 'my name is keen' SELECT SubString(@varTemp,0,20) AS [var char] --PatIndex():用于返回指定表达式中某模式第一次出现的起始为位置 DECLARE @temp varchar(200) SET @temp = 'my name is keen' SELECT PATINDEX('%keen%',@temp)--12 SELECT b_code AS '书号', '$'+CAST(b_price AS varchar) AS '单价', '$'+CONVERT(VARCHAR(20),sal_tot,1) AS '销售总额', SubString(b_intro,(PATINDEX('%内容介绍:%',b_intro)),100) AS '内容介绍', FROM Bookinfo AS b,Sales AS s select * from Bookinfo --使用iif()函数根据指定条件显示查询结果 /* IIF(expr,truepart,falsepart) expr为真时,返回truepart,否则返回falsepart */ SELECT IIF(2>3,1,2)--2 --利用查询结果集生成表/临时表 SELECT * INTO #Bookinfo FROM Bookinfo SELECT * FROM #Bookinfo DROP TABLE #Bookinfo select * from sys.sysobjects order by name if(Exists(Select * From sys.SysObjects Where Name='dbo.Bookinfo')) begin print '存在表' end --判读某个表是否存在,存在时执行删除 select * from sys.sysobjects CREATE TABLE testTale ( name varchar, id int ) if(Exists(Select * From sys.SysObjects Where Name='testTale')) DROP TABLE testTale --或者 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[testTale]') --得出系统给表testTale分配的唯一ID and OBJECTPROPERTY(id, N'IsUserTable') = 1)-- 该对象的属性是表类型的 drop table [dbo].[testTale] /* 说明: object_id():返回对象标识号。参数:对象名称(nvarchar(128))。返回integer objectproperty():返回对象属性的属性值。参数:对象ID(integer),属相名称(varchar)。返回integer N:表示UNICODE类型,可以支持不同语种的对象名 */
/*selete:where子句过滤*/ --AND和OR运算符 USE db_sql2000 GO SELECT * FROM tb_xsb02 SELECT * FROM tb_xsb02 WHERE 商品名称='铂金吊坠' OR 商品名称='18K手链' AND 进价=400 SELECT * FROM tb_xsb02 WHERE(商品名称='铂金吊坠' OR 商品名称='18K手链') AND 进价=400 --比较运算符 --使用IN运算符给出查询范围 SELECT * FROM tb_TeacherInfo02 --使用IN查询数据 SELECT * FROM tb_TeacherInfo02 WHERE 教师姓名 IN('贯大红','房大伟') --使用IN查询数据 SELECT * FROM tb_TeacherInfo02 WHERE 年龄 IN(27,28) --使用IN查询数据 SELECT * FROM tb_TeacherInfo02 WHERE 年龄 IN(28) --使用NOT IN查询数据 SELECT * FROM tb_TeacherInfo02 WHERE 教师姓名 NOT IN('贯大红','房大伟') --使用BETWEEN/NOT BETWEEN .. AND 查询范围数据 SELECT * FROM tb_xsb02 --使用BETWEEN .. AND 查询 SELECT * FROM tb_xsb02 WHERE 进价 BETWEEN 200 AND 2000 --使用NOT BETWEEN .. AND 查询 SELECT * FROM tb_xsb02 WHERE 进价 NOT BETWEEN 200 AND 2000 --利用LIKE并引用通配符、转义字符并进行模糊查询 SELECT 1 WHERE '[abcd' LIKE '[[]%' select 1 where '^ABCDE' like '!^ABCDE' escape '!' select 1 where '^ABCDE' like '#^%' escape'#' select 1 where '[^A-Z]ABCDE' like '\[\^A\-Z\]%' escape '\'