知识在于积累(.NET之路……)

导航

文章分类 -  数据库

sql 将值转换为ascii来排序
摘要:--将后面两们字符转换为ascii值来排序select code,substring(code,1,len(code)-2)+CAST(ascii(substring(code,len(code)-1,1)) AS varchar(5))+CAST(ascii(substring(code,len(wbsNo),1)) AS code(5))from table1 where left(code,6)='030101' order by substring(code,1,len(code)-2)+CAST(ascii(substring(code,len(code)-1,1)) 阅读全文

posted @ 2012-12-26 16:26 汤尼 阅读(919) 评论(0) 推荐(0)

sql 递归查询(转)
摘要:☆ 获取数据库所有表名,表的所有列名 select name from sysobjects where xtype='u' select name from syscolumns where id=(select max(id) from sysobjects where xtype='u' and name='表名') ☆ 递归查询数据 Sql语句里的递归查询 SqlServer2005和Oracle 两个版本 以前使用Oracle,觉得它的递归查询很好用,就研究了一下SqlServer,发现它也支持在Sql里递归查询举例说明:SqlServ 阅读全文

posted @ 2012-10-26 11:36 汤尼 阅读(222) 评论(0) 推荐(0)

Delete,Update与left join
摘要:--UPDATE:UPDATE A SET ApproverID=NULL FROM [SH_MaterialApplyBuyBill] A LEFT JOIN [SH_MaterialApplyBuyBillDetail] B ON A.ID=B.[MaterialApplyBuyBillID]WHERE A.id=125 AND @InDetailCount=0--DELETE:DELETE A FROM [SH_ClosingBalance] A LEFT JOIN [SH_StoreHouse] B ON A.StoreHouseID=B.ID WHERE B.departmentID 阅读全文

posted @ 2012-08-01 16:57 汤尼 阅读(817) 评论(0) 推荐(0)

Sql Server日期时间转字符串
摘要:日期时间转字符串 Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CONVERT(varchar(100), GETDATE(), 1): 05/16/06 Select CONVERT(varchar(100), GETDATE(), 2): 06.05.16 Select CONVERT(varchar(100), GETDATE(), 3): 16/05/06 Select CONVERT(varchar(100), GETDATE(), 4): 16.05.06 Select CONVERT(v 阅读全文

posted @ 2011-04-28 09:26 汤尼 阅读(126) 评论(0) 推荐(0)

多表一对多关系的字段拼接的经典存储过程
摘要:比如说一个学校有多个学院,有学院数据表table1如下:============================IDName----------------------------1中文学院2外国语学院3计算机学院4社会科学学院============================然后每个学院都会有多个专业,有专业表table2如下:==================================IDXueYuanIdZYName11汉语专业21汉语专业二31汉语专业三43信息管理专业53软件专业63计算机信息专业====================================现 阅读全文

posted @ 2010-08-05 11:19 汤尼 阅读(328) 评论(0) 推荐(0)

SQL语句之父子分类关系的查询
摘要:例子如下图: 查询出来的结果多加一列,这一列的值为,当icode_ind有子分类,则该列的值为1,否则为0。是否有子类,看一下那表就很明显我的规则了。 实现的SQL语句: 1.Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->select a.iCode_ind,a.icode,case when b.iCode_ind is null then 0 else 1 endfrom TabA a outer apply (select top 1 阅读全文

posted @ 2010-08-02 09:54 汤尼 阅读(487) 评论(0) 推荐(0)

Excel 导入数据到SQL Server 数据库
摘要:方法一:Excel导入SQL Server数据库,要求Excel表里的字段跟数据库的字段要匹配代码Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebContro 阅读全文

posted @ 2010-04-28 18:02 汤尼 阅读(309) 评论(0) 推荐(0)

实用的SQL语句集
摘要:有两个表t1和t2,如下所示:表t1:select * from t1表t2:select * from t2如果SQL语句为:select 0 as cid,'未指定' as name UNION ALL select cid,name from t1则得如下数据:如果SQL语句为:select *,'' as col1 from t1select *,'a' as col1 from t1select *,2 as col1 from t1将分别得到如下三种数据: 阅读全文

posted @ 2010-04-16 17:35 汤尼 阅读(108) 评论(0) 推荐(0)

一个经典的存储过程
摘要:代码Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->CREATE PROCEDURE [dbo].[PageGeneral] ( @pagesize int, @pageindex int, @docount bit, @TableName varchar(50), --表名 @Fields varchar(5000) = '*', --字段名(全部字段为*) @OrderField varchar(5000), --排序字段 阅读全文

posted @ 2010-04-14 13:46 汤尼 阅读(166) 评论(0) 推荐(0)

链接sql server 数据库的字符串小结
摘要:链接sql server 20001、安全标准:Data Source=服务器地址;Initial Catalog=数据库名;User Id=登陆用户名;Password=密码;Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;2、标准的安全替代的语法:Ser 阅读全文

posted @ 2010-04-13 15:19 汤尼 阅读(183) 评论(0) 推荐(0)

SQL SERVER自定义函数实例(一)
摘要:SQL SERVER自定义函数。功能如下:输入两个参数@param1、@param2,这两个参数的类型均为varchar。在表里,这两个字段的值有很多种形式,比如:“¥10.33”、“¥10.33元”、“¥10.33万元”、“一串文本”、NULL、空字符串我要计算@param1/@param2在计算之前我先进行数据判断。即只有“¥10.33”、“¥10.33元”、“¥10.33万元”这三种形式的数据方可参与计算,但在计算之前,又得进行数据提取,如提取10.33来进前计算,但如果有“万元”,则该数据要先*10000,化为元才能参加计算。最后返回一个varchar型的值.实现代码如下:代码Code 阅读全文

posted @ 2010-04-09 18:19 汤尼 阅读(557) 评论(0) 推荐(0)

sql 类型转换函数
摘要:select cast(100+99 as char) convert(varchar(12), getdate()) 运行结果如下 ------------------------------ ------------ 199 Jan 15 2000 ----日期函数---- DAY() --函数返回date_expression 中的日期值 MONTH() --函数返回date_expression 中的月份值 YEAR() --函数返回date_expression 中的年份值 DATEADD(<datepart> ,<number> ,<date>) 阅读全文

posted @ 2010-04-09 13:33 汤尼 阅读(1358) 评论(0) 推荐(0)