|
|
|
|
|
|
随笔分类 -
SQL
游标的类型
摘要:依照游标的功能来分,可以分为四种。 静态游标 静态游标 (Static Cursor) 是利用暂存资料表作为储存结果集空间的一种游标,它可以让应用程式可以快速的存取结果集,但在静态游标开启期间,任何对资料表所做的变更都不会反映在结果集中;同时,在静态游标中所作的修改,无法反映到资料库中,此种游标是消耗资源度第三的游标。 Defines a cursor that makes a temporary...
阅读全文
使用C# Detach和Attach 数据库
摘要:先上一个使用SQL 语句进行Detach和Attach数据库的语句: use mastergosp_detach_db 'TestDB'go use mastergosp_attach_db '1','C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\TestDB.mdf','C:\Program Files\...
阅读全文
查看死锁的SQL语句
摘要:use master go declare @spid int,@bl int DECLARE s_cur CURSOR FOR select 0 ,blocked from (select * from sysprocesses where blocked>0 ) a where not exists(select * from (select * from sysprocesses ...
阅读全文
使用SQL SERVER的Link Server实现分布式事务
摘要:ansi_warning和ansi_nulls 两个选项必须打开,好像对系统也没啥影响。 楼主再检查一下两个地方,经测试,在要使用分布式事务时,这是必须要设置的。 1、链接服务器和名称解析问题 -- 创建链接服务器 EXEC sp_addlinkedserver 'srv_lnk', 'ms', 'SQLOLEDB', '192.168.1.1' GO -- 创建链接服务器上远程登录之间...
阅读全文
SQL Server链接服务器 Linked Server
摘要:使用方法[代码]例如:[代码]Oracle作为远程链接服务器时,SQL Server服务器上必须安装Oracle Client。配置SQL*Net名供OLE DB provider使用sp_addlinkedserver各种provider参考sp_addlinkedserverSQL Server对OLE DB provider的支持情况可参考OLE DB providers Tested wi...
阅读全文
生成日期,周,月,季度,年,财年关系表
摘要:declare @date datetime , @hour int , @min int , @fiscalyear int set @date = '1/1/2006' set @hour = 0 set @min = 0while (@date <= '1/1/2020') begin set @fiscalyear = (datepart(year,dateadd(month,(6)...
阅读全文
代码调用存储过程超时,SQL Server Management Studio里运行很快
摘要:今天同事用代码调用存储过程时超时,在SQL Server Management Studio里运行却很快就有结果,这和我上次遇到的情况一下,现在将解决方案记录如下,谨为大家作一提醒。 解决方案就是: 把存储过程中多余的空格删掉。 瞠目吧结舌吧,不过这就是现实,现实是残酷的,现实是无情的,哈哈 不一定是哪个空格就引起了超时,试着看吧 2009/09/08: 同事又报怨ADO.net执行时超时异常了,...
阅读全文
SQL语句生成表结构
摘要:CREATE PROCEDURE sp_ScriptTable( @TableName SYSNAME, @IncludeConstraints BIT = 1, @IncludeIndexes BIT = 1, @NewTableName SYSNAME = NULL, @UseSystemDataTypes BIT = 0)ASBEGIN DECLARE @MainDefinition TAB...
阅读全文
SQL 字串分割函数
摘要:create function [dbo].[split](@input varchar(max), @split varchar(max)) returns @table table(col varchar(max)) as begin while(charindex(@split,@input)<>0) begin insert @table(col) values (ltrim(...
阅读全文
SQL Server 分布式查询
摘要:连sql server默认实例用:exec sp_addlinkedserver @ServerName, N'SQL Server'连指定实例用:EXEC sp_addlinkedserver @server='SQL2005', @srvproduct='', @provider='SQLNCLI', @datasrc='DBhere' @server 机器名 @provider (Use S...
阅读全文
SQL Server 2008 端口配置
摘要:若SQL Server 2008 服务器启用了windows 防火墙,其他机器就不能通过SQL Server Management Studio连接. 需要在windows 防火墙中增加1433的TCP端口, 如下图: 图。。。
阅读全文
SQL Server 2008中的merge
摘要:在 SQL Server 2008 中,通过使用 MERGE 语句,可以在单个语句中执行多个数据操作语言 (DML) 操作。例如,您可能需要根据在另一个表中找到的差异在一个表中插入、更新或删除行,从而对两个表进行同步。通常,可以通过执行包含各个 INSERT、UPDATE 和 DELETE 语句的存储过程或批处理来实现这一目的。然而,这意味着需要多次计算和处理源表和目标表中的数据;至少对每个语句计...
阅读全文
查找表中重复数据
摘要:查找表中重复数据:select * from channel where channelname in ( select channelname from channel group by channelname having (count(*) > 1) ) 查找channel表中channelname重复的所有数据。having也是很有用的啊
阅读全文
在 SQL Server 中使用 Transact-SQL如何遍历一个结果集
摘要:there are can use to iterate through result set by using Transact-SQL statements three methods。 一种方法是使用 临时 表。 使用此方法,您创建初始 SELECT 语句的"快照"并将其用作基础的"指针"。 例如: /********** example 1 **********/ declare @au_id char( 11 )set rowcount 0select * into #mytemp from authorsset rowcount 1selec
阅读全文
IDENT_CURRENT ,@@IDENTITY, SCOPE_IDENTITY
摘要:IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope. @@IDENTITY returns the last identity value generated for any table in the current session, ac...
阅读全文
数据集分组并且组内部排序
摘要:假设有表如下(仅作示例): create table dbo.Score( ScoreId int identity(1,1) , Student nvarchar(max) , SubjectId nvarchar(max) , ScoreNumber int ) insert into Score values('A', '数学', 96)insert into Score values('A', '语文', 90)insert into Score values('A', '程序设计', 77
阅读全文
从SQLServer 返回树型Xml
摘要:数据库表结构如下 要得到 Channel account campaign 这样的树型Xml结构, SQL 语句如下:输出结果如下几点说明:We can leverage the new TYPE directive to generate XML data type instances (otherwise, you will get a textual result that will b...
阅读全文
SQL中on条件与where条件的区别
摘要:数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。 在使用left jion时,on和where条件的区别如下: 1、 on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。 2、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真...
阅读全文
存储过程中使用函数作为默认参数
摘要:create procedure [dbo].[InsertRequestLog]( @SearchEngineID tinyint , @RequestID bigint , @TaskTypeID tinyint , @RequestObjectID tinyint , @Status tinyint , @SubmitTime datetime = getdate , @LastMo...
阅读全文
|
|