0
背景是这样。需要找到当前数据库里面所有column,但想排除某些特定的column,而且希望这个是能够通过配置即可自动实现而不需要改query。关键就在创建这个配置表,并且LIKE的使用。
首先创建一个#tmp的临时表,写入配置。此后将临时表的value写到LIKE之后。
想了一下,用到这种方法的情况挺少的,蛮有意思。
Posted on
Wednesday, January 07, 2015
by
醉·醉·鱼
and labeled under
sql
还是第一次遇到column LIKE column的情况。背景是这样。需要找到当前数据库里面所有column,但想排除某些特定的column,而且希望这个是能够通过配置即可自动实现而不需要改query。关键就在创建这个配置表,并且LIKE的使用。
首先创建一个#tmp的临时表,写入配置。此后将临时表的value写到LIKE之后。
想了一下,用到这种方法的情况挺少的,蛮有意思。
0
Posted on
Tuesday, January 06, 2015
by
醉·醉·鱼
and labeled under
sql
做数据修复的时候,有时候需要动态生成rollback script。下面就是用AdventureWorks2012做个例子自动生成rollback script。
USE AdventureWorks2012;
DECLARE @personId int = 1
CREATE TABLE #tmp_person(
BusinessEntityID int,
PersonType nchar(2),
NameStyle bit,
Title nvarchar(8),
FirstName nvarchar(50),
MiddleName nvarchar(50),
LastName nvarchar(50),
Suffix nvarchar(10),
EmailPromotion int,
AdditionalContactInfo xml,
Demographics xml,
rowguid uniqueidentifier,
ModifiedDate datetime);
CREATE TABLE #tmp_address(
AddressID int,
AddressLine1 nvarchar(60),
AddressLine2 nvarchar(60),
City nvarchar(30),
StateProvinceID int,
PostalCode nvarchar(15),
SpatialLocation geography,
rowguid uniqueidentifier,
ModifiedDate datetime);
INSERT INTO #tmp_person
SELECT TOP 1 *
FROM Person.Person
WHERE BusinessEntityID = @personId
--delete data
--DELETE FROM Person.Person WHERE BusinessEntityID = @personId
PRINT 'ROLLBACK SCRIPT'
PRINT '----------------------------------------'
SET NOCOUNT ON
CREATE TABLE #tables (id int identity PRIMARY KEY, table_name varchar(50), tmp_table_name varchar(50))
--tables to insert the data back
INSERT INTO #tables(table_name, tmp_table_name) VALUES ('Person.Person', '#tmp_person')
INSERT INTO #tables(table_name, tmp_table_name) VALUES ('Person.Address', '#tmp_address')
DECLARE @columns NVARCHAR(MAX) = N'';
DECLARE @sql NVARCHAR(MAX) = N'';
DECLARE @tablename VARCHAR(50),
@tmptablename VARCHAR(50),
@rowcount int
------------------- AdventureWorks2012 -------------------
PRINT 'USE AdventureWorks2012;'
DECLARE table_cursor CURSOR FAST_FORWARD LOCAL
FOR
SELECT table_name, tmp_table_name
FROM #tables
ORDER BY id
OPEN table_cursor
FETCH NEXT FROM table_cursor
INTO @tablename, @tmptablename
WHILE @@FETCH_STATUS = 0
BEGIN
--Check if data exists in temp table. Skip if no data
SET @sql = N'SELECT 1 FROM ' + @tmptablename
EXEC(@sql)
SET @rowcount = @@rowcount
IF @rowcount <> 0
BEGIN
-- get columns names to be inserted
SELECT @columns = STUFF(
(SELECT '+'''''', '''''' + ISNULL(CAST(' + name + ' AS varchar(2000)), ''NULL'')' FROM sys.columns c
WHERE object_id = object_id(@tablename)
ORDER BY c.column_id
FOR XML PATH('')
), 1, 12, '')
-- format INSERT script
SET @sql = N'SELECT @sqlout = STUFF(
(SELECT N''INSERT INTO ' + @tablename + ' VALUES ('''''' + ' + @columns + N' + N''''''); ''
FROM ' + @tmptablename +'
FOR XML PATH('''')
), 1, 0, '''')'
EXEC sys.sp_executesql @sql, N'@sqlout NVARCHAR(MAX) OUTPUT', @sqlout = @sql OUTPUT
SET @sql = REPLACE(@sql, '''NULL''', 'NULL')
SET @sql = REPLACE(@sql, 'INSERT INTO', CHAR(10)+'INSERT INTO')
PRINT @sql
END
FETCH NEXT FROM table_cursor
INTO @tablename, @tmptablename
END
CLOSE table_cursor
DEALLOCATE table_cursor
0
http://mitchelsellers.com/blogs/2011/09/23/sql-server-forward_only-cursor-for-performance.aspx
在这里,MSDN更加详细地解释了FAST_FORWARD.
http://blogs.msdn.com/b/sqlqueryprocessing/archive/2009/08/12/understanding-sql-server-fast-forward-server-cursors.aspx
Posted on
Monday, January 05, 2015
by
醉·醉·鱼
and labeled under
sql
在寻找一些关于SQL SERVER CURSOR FAST_FORWARD的文章,发现这么一个案例。都知道用游标可能会导致运行速度很慢,但又如何去处理呢?在这个案例里面,他需要处理近60000条记录,每条记录又会衍生更多的操作等等。在添加上FAST_FORWARD之后情况,情况就好了很多。http://mitchelsellers.com/blogs/2011/09/23/sql-server-forward_only-cursor-for-performance.aspx
在这里,MSDN更加详细地解释了FAST_FORWARD.
http://blogs.msdn.com/b/sqlqueryprocessing/archive/2009/08/12/understanding-sql-server-fast-forward-server-cursors.aspx
0
[Source Information]
Source Location : .
Source Provider : SQLNCLI11
Table: [Query]
Column: VariableCharacterField
Column Type: 200
SSIS Type: (Type unknown ...)
Mapping file (to SSIS type): C:\Program Files (x86)\Microsoft SQL Server\110\DTS\MappingFiles\MSSQLToSSIS10.XML
官方已经在SQL SERVER 2012 SP2里面修好了问题。不想升级的话,有个work around就是自己去更改MSSQLToSSIS10.XML文件。具体见
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/97ff1f01-c02a-4c9a-b867-8eaecc464cfb/2012-sp1-no-longer-recognizes-common-data-types?forum=sqlintegrationservices
官方文档https://connect.microsoft.com/SQLServer/feedback/details/772761/dtswizard-in-sql-2012-sp1-no-longer-recognizes-nvarchar-varchar-data-types-when-source-is-a-query
Posted on
Wednesday, October 22, 2014
by
醉·醉·鱼
and labeled under
sql
[Source Information]
Source Location : .
Source Provider : SQLNCLI11
Table: [Query]
Column: VariableCharacterField
Column Type: 200
SSIS Type: (Type unknown ...)
Mapping file (to SSIS type): C:\Program Files (x86)\Microsoft SQL Server\110\DTS\MappingFiles\MSSQLToSSIS10.XML
官方已经在SQL SERVER 2012 SP2里面修好了问题。不想升级的话,有个work around就是自己去更改MSSQLToSSIS10.XML文件。具体见
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/97ff1f01-c02a-4c9a-b867-8eaecc464cfb/2012-sp1-no-longer-recognizes-common-data-types?forum=sqlintegrationservices
官方文档https://connect.microsoft.com/SQLServer/feedback/details/772761/dtswizard-in-sql-2012-sp1-no-longer-recognizes-nvarchar-varchar-data-types-when-source-is-a-query
0
简而言之,LEFT OUTER JOIN是先进行JOIN,拿出所有match的records,然后再做过滤。而NOT EXISTS是一旦有匹配就进行下一个比对。
这里有很详细的解释。
http://sqlinthewild.co.za/index.php/2010/03/23/left-outer-join-vs-not-exists/
http://stackoverflow.com/questions/6777910/sql-performance-on-left-outer-join-vs-not-exists
Posted on
Monday, October 20, 2014
by
醉·醉·鱼
and labeled under
sql
源于一次数据库的面试题目,老板对别人的答题给了90分,我看了一下,没有看出理由。后来查了一下,原来如此。简而言之,LEFT OUTER JOIN是先进行JOIN,拿出所有match的records,然后再做过滤。而NOT EXISTS是一旦有匹配就进行下一个比对。
这里有很详细的解释。
http://sqlinthewild.co.za/index.php/2010/03/23/left-outer-join-vs-not-exists/
http://stackoverflow.com/questions/6777910/sql-performance-on-left-outer-join-vs-not-exists
0
http://msdn.microsoft.com/en-us/library/ms190273.aspx
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/6784c3d8-2e40-47fd-956a-910c576a2266/changing-column-in-not-null-why-is-there-so-much-transaction-log-activity-on-compressed-tables?forum=sqldatabaseengine
此外,它依旧还是会有3个操作,所以http://dba.stackexchange.com/a/29526/37769 里面描述的依旧有效。
Posted on
Monday, October 20, 2014
by
醉·醉·鱼
and labeled under
sql
从SQL SERVER 2012开始,Alter table column from NULL to NOT NULL with default value is metadata change.http://msdn.microsoft.com/en-us/library/ms190273.aspx
除了文中说的例外,data compression也会有影响的。所以,还是得小心啊。Adding NOT NULL Columns as an Online Operation
Starting with SQL Server 2012 Enterprise Edition, adding a NOT NULL column with a default value is an online operation when the default value is a runtime constant. This means that the operation is completed almost instantaneously regardless of the number of rows in the table. This is because the existing rows in the table are not updated during the operation; instead, the default value is stored only in the metadata of the table and the value is looked up as needed in queries that access these rows. This behavior is automatic; no additional syntax is required to implement the online operation beyond the ADD COLUMN syntax. A runtime constant is an expression that produces the same value at runtime for each row in the table regardless of its determinism. For example, the constant expression "My temporary data", or the system function GETUTCDATETIME() are runtime constants. In contrast, the functions NEWID() or NEWSEQUENTIALID() are not runtime constants because a unique value is produced for each row in the table. Adding a NOT NULL column with a default value that is not a runtime constant is always performed offline and an exclusive (SCH-M) lock is acquired for the duration of the operation.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/6784c3d8-2e40-47fd-956a-910c576a2266/changing-column-in-not-null-why-is-there-so-much-transaction-log-activity-on-compressed-tables?forum=sqldatabaseengine
此外,它依旧还是会有3个操作,所以http://dba.stackexchange.com/a/29526/37769 里面描述的依旧有效。
- 创建一个新的column
- 为新的column赋值
- 将前一个column标记dropped