随笔分类 -  Sql Server

摘要:转自http://edu.codepub.com/2011/0426/31119.phpifexists(select*fromdbo.sysobjectswhereid=object_id(N'[dbo].[p_exporttb]')andOBJECTPROPERTY(id,N'IsProcedure')=1)dropprocedure[dbo].[p_exporttb]GOcreateprocp_exporttb@sqlstrsysname,--查询语句,如果查询语句中使用了orderby,请加上top100percent,注意,如果导出表/视图,用上面的存 阅读全文
posted @ 2011-08-15 11:04 再快一点 阅读(4865) 评论(0) 推荐(0)
摘要:在Sql连接字符串中设置Connect Timeout无效,只能通过Socket去判断代码如下:///<summary>///测试数据库服务器是否可用///</summary>///<paramname="ip">服务器IP</param>///<paramname="port">服务器端口,一般是1433</param>///<paramname="timemout">设置超时时间,毫秒</param>///<returns> 阅读全文
posted @ 2011-07-25 11:36 再快一点 阅读(368) 评论(0) 推荐(0)
摘要:create proc p_test@total int =1 outputasbegin if(@total=1) begin return end select * from test where uid=@totalendSqlDataAdapter sda = new SqlDataAdapter("p_test", conn); sda.SelectCommand.CommandType = CommandType.StoredProcedure; SqlParameter pTotal = new SqlParameter("@total", 阅读全文
posted @ 2011-06-17 17:19 再快一点 阅读(237) 评论(0) 推荐(0)
摘要:sql读取excel:select * from opendatasource( 'Microsoft.Jet.OLEDB.4.0', 'Data Source=F:\test.xls;Extended Properties=''Excel 8.0;HDR=Yes;IMEX=1''')...sheet1$ 阅读全文
posted @ 2011-05-12 14:37 再快一点 阅读(319) 评论(0) 推荐(0)
摘要:sql server 20001.dbcc traceon(1205)dbcc traceon(3605)或者dbcc traceon(1205,3605) 备注 使用后者,则死锁的日志记录信息比前者多2.dbcc traceon(1204)dbcc traceon(3605)或者dbcc traceon(1204,3605) 备注使用两者死锁的日志记录信息比前者一样3.dbcc traceon(1204,1205,3605) 日志记录比 dbcc traceon(1205,3605)还多 阅读全文
posted @ 2011-05-11 23:28 再快一点 阅读(212) 评论(0) 推荐(0)
摘要:返回一个10-1000的随机数:declare @M int,@N intset @N=10set @M=1000declare @t int declare @i intset @i=0while(@i<10000)begin select @t=cast((rand()*(@M-@N+1)+@N) as int) if(@t=10) begin select @t end set @i=@i+1end返回一个10-999的随机数: declare @M int,@N intset @N=10set @M=1000declare @t int declare @i intset @i= 阅读全文
posted @ 2011-04-26 10:22 再快一点 阅读(285) 评论(0) 推荐(0)
摘要:--proc_insert 'pet_acation',1 /* 根据网上资料修改,完成于 2006-6-26 增加对自增列的支持 */ alter proc proc_insert ( @tablename varchar(256), @keepidentity tinyint =1,-- 是否保留identity原值 1:保留 0:不保留 @where varchar(1000)) as begin set nocount on declare @sqlstr varchar(8000) declare @sqlstr1 varchar(8000) declare @sql 阅读全文
posted @ 2011-04-19 21:13 再快一点 阅读(188) 评论(0) 推荐(0)
摘要:--用户投注表历史表create table t_Userinfo_his ( rid int identity, userid int not null, --用户ID nick varchar(50) not null, --用户昵称 termid int not null, --比赛ID nums int not null, --投注数字 instone bigint not null, --投入金额 outstone bigint not null, --返还金额 addtime datetime not null, --投注时间 )go--功能:统计每日盈利最多的用户--... 阅读全文
posted @ 2011-04-13 00:27 再快一点 阅读(171) 评论(0) 推荐(0)
摘要:1. 在存储过程开始时 set noucount on 结束时 set noucount off(设置nocount 为 ON 以后,只对当前链接一直有效(除非 set nocount off),对其他SQL会话无效)2.SET XACT_ABORT ON ,当 SET XACT_ABORT 为 ON 时,如果 Transact-SQL 语句产生运行时错误,整个事务将终止并回滚。为 OFF 时,只回滚产生错误的 Transact-SQL 语句,而事务将继续进行处理。编译错误(如语法错误)不受 SET XACT_ABORT 的影响。(设置XACT_ABORT 为 ON 以后,只对当前链接一直有效 阅读全文
posted @ 2011-04-01 11:30 再快一点 阅读(193) 评论(0) 推荐(0)
摘要:static void Main() { SqlParameter para = new SqlParameter("wq", 0); Console.WriteLine(para.SqlDbType);//输出BigInt, Console.WriteLine(para.Value==null);//输出true //难道new SqlParameter("wq", 0);调用的是SqlParameter(string parameterName, SqlDbType dbType)这个重载?微软的Bug? para.Value = 0; Consol 阅读全文
posted @ 2011-03-31 16:33 再快一点 阅读(167) 评论(0) 推荐(0)
摘要:1.排它锁在一个Sql连接中这样写:begin tran--select * from a with(UPDLOCK)update a set [name]='wq' where [id]=2 --这里的set的值不能不变(即不能本来name='wq'又set name='wq'),否则Sql Server会优化成不加锁waitfor delay '00:00:08'commit tran在另外一个sql连接中这样写:select * from a 发现第二个连接里的sql语句必须等到第一个连接里的事务完成才执行完成,这是因为第一 阅读全文
posted @ 2011-03-16 21:17 再快一点 阅读(4748) 评论(2) 推荐(1)
摘要:create table products( pid int primary key, [no] varchar(100) unique not null, pname varchar(100), shijian timestamp)insert products select 1,'pro1','张三'insert products select 2,'pro2','李四'insert products select 3,'pro3','王五'insert products select 4,'pro4','赵六'insert products select 5,'pro5','楚七'ins 阅读全文
posted @ 2010-11-24 16:59 再快一点 阅读(271) 评论(0) 推荐(0)
摘要:--用指定的字符拆分一个字符串,并传入一个字符串判断传入的这个字符串是否存在于拆分以后的字符串数组中create function SplitString(@expression varchar(500),@char varchar(10),@str varchar(50)) returns bit as begin --如果传进来的字符串是null则返回0 if(isnull(@expression,' ')=' ') return 0declare @beginIndex int,@endIndex int --截取子串的起始索引和结束索引 declare @Count int --记录循 阅读全文
posted @ 2010-11-10 10:31 再快一点 阅读(2109) 评论(0) 推荐(0)
摘要:SELECT distinct top 2 LastName FROM [dbo].[Employees] order by LastNamedistinct要放到top前面,这样取出的结果是先将数据Distinct去重复,然后再取其中的前两行 阅读全文
posted @ 2010-05-07 11:37 再快一点 阅读(12082) 评论(3) 推荐(2)
摘要:实际的开发可能会遇到数据大批量插入数据的问题,若是一条条的循环倒数效率非常低下,提供这个较好的解决方案 12protectedvoidButton1_Click(objectsender,EventArgse)3{45DateTimebeginTime=DateTime.Now;6Response.Write("开始时间:"+beginTime.ToString("yyyy年MM月dd日:HH:mm:ss:fff"));78//构造一个Datatable存储将要批量导入的数据9DataTabledt=newDataTable();10dt.Columns.Add("id",typeof(st 阅读全文
posted @ 2010-05-04 12:41 再快一点 阅读(334) 评论(0) 推荐(0)
摘要:今天花了两个小时,写了两种分页的算法。--not in删选法,这种方法查询某个表中前面的数据效率高create procedure Page_proc(@pageSize int, --页数,@pageIndex int,--页码@tableName varchar(50),--表名@mast varchar(50)--表的主键名)asbegindeclare @arg intset @arg=@pageSize*(@pageIndex-1)--使用动态Sqldeclare @strSql varchar(1000)set @strSql='select top('+cast( 阅读全文
posted @ 2009-11-25 16:09 再快一点 阅读(417) 评论(1) 推荐(0)