
SQL
EricYeung
这个作者很懒,什么都没留下…
展开
-
get curr month start date, last month end date
declare @d datetimeset @d = 2009-05-05select dateadd(dd, -day(@d)+1, @d) as curr_mth_start, dateadd(dd, -day(@d), @d) as last_mth_end原创 2009-05-05 11:38:00 · 664 阅读 · 0 评论 -
关于SQL Server的ISOLATION
ISOLATION 是Connection的一个property,每个session都有自己的Isolation Level1)Read Uncommitted (不做控制)能读别人已修改未提交的databegin traninsert(can read data)2)Read committed不能读别人已修改未提交的databegin traninsert直到commit(can read d原创 2007-12-10 08:34:00 · 892 阅读 · 0 评论 -
Locking in Microsoft SQL Server (Quoted from mssqlcity)
Locking in Microsoft SQL ServerAlexander Chigrikchigrik@mssqlcity.comIntroductionTransaction Isolation LevelsLock typesLocking optimizer hintsDeadlocksView locks (sp_lock)LiteratureIntroductionIn this转载 2007-12-10 08:25:00 · 996 阅读 · 0 评论 -
Functional Dependencies & MultiValued Dependency
Functional Dependencies (FDs) 1->1A->B: If I know your vendor code, I can determine the vendor nameMultiValued Dependency (MVD) 1 -> *A->->B: If I know the customer code, I can determine the list of t原创 2007-12-10 10:16:00 · 943 阅读 · 0 评论 -
SQL 2005 DataWarehouse Note
@2007 June 02MS BI Studiofact table (transaction table)dim table (master file: product, customer)BI Studio1)Datasource: Delphi ADOConnection2)Datasource View: ADOTable3)Cube4)Dimensionmainly involve:原创 2007-12-10 09:15:00 · 739 阅读 · 0 评论 -
SQL tree data struct(7): find the minimal cost route
-- find the minimal cost routecreate table flights(departure char(20), destination char(20), cost int)insert flights select Chicago, New York, 10 unionselect Chicago, Milwaukee, 20 unionselect原创 2008-03-02 11:54:00 · 829 阅读 · 0 评论 -
SQL & Regex(3): subset construction
-- Subset Construction: convert NFA to DFA-- require: NFA_trans_table is filled with NFA state data-- ========================================================================exec clearobj DFA_states原创 2008-03-02 11:39:00 · 737 阅读 · 0 评论 -
SQL tree data struct(6): NestedSet2AdjList
declare @personnel table(emp varchar(30), lft int, rgt int)insert @personnelselect Albert, 1, 12union all select Bert, 2, 3 union all select Chuck, 4, 11 union all select Donna, 5, 6 union all原创 2008-03-02 11:52:00 · 980 阅读 · 0 评论 -
SQL tree data struct(2): materialized path (1.2.3...)
/*then materialized path representation of tree is easy to output: order by path, level*/declare @orgchart table (emp varchar(30), path varchar(100))insert @orgchartselect Albert, 1union all selec原创 2008-03-02 11:47:00 · 1011 阅读 · 0 评论 -
clear sql server object by name
if object_id(clearobj) is not null drop proc clearobjgocreate proc clearobj @obj nvarchar(50), @debug int = 0with encryptionas declare @sql nvarchar(256) select @sql = drop + case (s原创 2008-03-02 11:44:00 · 570 阅读 · 0 评论 -
SQL & Regex(4): usage
-- ========================================================================exec clearobj DFA_simulate, 1gocreate proc DFA_simulate @str nvarchar(100), @match bit outputwith encryptionasdeclare @i原创 2008-03-02 11:40:00 · 612 阅读 · 0 评论 -
SQL & Regex(2): thompson construction
-- Thompson Construction : convert Regular Expression Parse Tree to NFA-- require: parse_node table is filled with tree-struct data-- ==================================================================原创 2008-03-02 11:38:00 · 673 阅读 · 0 评论 -
Database normal form
1 NF=====================================================================Acolumn is atomic, not array, no repeating semantic columnsanti-example:Customers(CustID INT, CustName VARCHAR(30)...)SQL 2005原创 2007-12-10 10:15:00 · 639 阅读 · 0 评论 -
SQL & Regex(1): parse tree
-- convert regular expression string to parse treeif object_id(clearobj) is not null drop proc clearobjgocreate proc clearobj @obj nvarchar(50), @debug int = 0with encryptionas declare @sql nvarc原创 2008-03-02 11:37:00 · 1082 阅读 · 0 评论 -
SQL tree data struct(1): adjacency list (id, parentid)
declare @tree table(emp varchar(30), boss varchar(30))insert @treeselect Albert, NULLunion all select Bert, Albertunion all select Chuck, Albertunion all select Donna, Chuckunion all sel原创 2008-03-02 11:46:00 · 677 阅读 · 0 评论 -
Calc open position
USE tempdbgoIF object_id([tb]) IS NOT NULL DROP TABLE [tb]goCREATE TABLE [tb]( [id] int, [amt] int)INSERT [tb]SELECT 1, 10UNION ALLSELECT 2, -8UNION ALLSELECT 3, 7UNION AL原创 2008-12-23 10:33:00 · 566 阅读 · 0 评论 -
get last date record of each product
use tempdbgoselect *into #tempfrom (select apple as product, 2008-10-02 as saledate, 87 as qtyunion all select apple, 2008-10-03, 34union all select apple, 2008-11-17, 75union all select原创 2009-02-04 20:22:00 · 530 阅读 · 0 评论 -
sql split path
declare @str varchar(100)set @str = C:/data/test.csvdeclare @idx int, @len intset @len = len(@str)set @idx = @lenwhile(@idx > 0 and substring(@str, @idx, 1) if @idx - 1 > 0 print path = + sub原创 2008-07-16 11:41:00 · 588 阅读 · 0 评论 -
SQL: select @var = xxx from table1 problem
declare @var intset @var = 1print @var = + isnull(str(@var), null)select top 1 @var = [object_id] from sys.objects where 1=2-- @var != nullprint @var = + isnull(str(@var), null)原创 2008-07-16 11:33:00 · 828 阅读 · 0 评论 -
Improve performance using hash keys
use CheckSum function to turn a string key to a hash key(integer), this will improve performance原创 2008-03-11 09:40:00 · 562 阅读 · 0 评论 -
SQL Server 2005 error handling
Extract from : SQL Server Error Handling Workbenchhttp://www.simple-talk.com/sql/t-sql-programming/sql-server-error-handling-workbench/--Catch Error:--=============================================BEGI原创 2008-03-11 09:48:00 · 787 阅读 · 0 评论 -
Compare 2 string with case sensitive
Select * from test where convert(varbinary, column1) = convert(varbinary, ABCD)原创 2008-03-11 09:35:00 · 645 阅读 · 0 评论 -
SQL tree data struct(4): AdjList2MatPath (for printing tree struct)
if object_id(proc_print_tree) is not null drop procedure proc_print_treegocreate procedure proc_print_tree @raw_data_sql nvarchar(1000), @debug bit = 0with encryptionas if object_id(tempdb..#原创 2008-03-02 11:51:00 · 873 阅读 · 0 评论 -
SQL tree data struct(3): nested set (1,12) include (2,3) & (4, 11) ...
-- Credit to JOE CELKO-- http://archives.postgresql.org/pgsql-sql/2001-11/msg00004.php-- CREATE TABLE Personnel -- (emp CHAR(10) NOT NULL PRIMARY KEY, -- lft INTEGER NOT NULL UNIQUE CHECK (lft > 0原创 2008-03-02 11:49:00 · 850 阅读 · 0 评论 -
SQL read MS access(*.mdb)
select CustID, CustName from openrowset(Microsoft.Jet.OLEDB.4.0, C:/trade.mdb;Admin;, Customer)原创 2008-03-02 11:56:00 · 621 阅读 · 0 评论 -
SQL tree data struct(5): AdjList2NestedSet
/*The organizational chart would look like this as a directed graph: Albert (1,12) / / / / Bert (2,3) Chuck (4,11) / | /原创 2008-03-02 11:52:00 · 880 阅读 · 0 评论 -
T-SQL Nested procedure & transactions
IF OBJECT_ID(sp_ThrowExp) IS NOT NULL DROP PROC sp_ThrowExpGOCREATE PROC sp_ThrowExpAS RAISERROR(Error occurs., 16, 1)GOIF OBJECT_ID(sp_OuterProc) IS NOT NULL DROP PROC sp_OuterProcGOC原创 2007-12-06 14:31:00 · 653 阅读 · 0 评论 -
Get datepart
SELECT DATEADD(DAY,0, DATEDIFF(DAY,0, GETDATE()))SELECT CAST(CAST(GETDATE() AS INT) AS DATETIME)原创 2008-02-25 14:08:00 · 616 阅读 · 0 评论 -
在当前数据库的所有表,所有字段查找字符串
/* search a string in all databaes tables and all fields*/if object_id(search_db) is not null drop proc search_dbgocreate proc search_db @table nvarchar(100), @cond nvarchar(512)asdeclar原创 2006-05-10 13:37:00 · 1588 阅读 · 0 评论 -
数据库表行列转换
use pubsgodeclare @table_name varchar(35), -- if the table have too much fields ,this proc may be malfunction @column_list nvarchar(4000) -- field list, separated with ,select @table_name原创 2006-05-10 12:32:00 · 1123 阅读 · 0 评论 -
Install sql scripts using batch(osql)
:: install all .sql file into local SQL server:: database=pubs, user=sa, password=echo offfor %%f in (*.sql) do (echo %%f & osql /n /S. /Usa /P /dpubs /i%%f & echo - - - - - -)echo COMPLETEDif "%1原创 2006-05-05 13:28:00 · 1034 阅读 · 0 评论 -
Compute the region of specific value inside a dataset
-- take the data below for example, we can see that records from 2 to 3 is 0, and records from 8 to 10 is also 0,-- and we want to answer this question using SQLdeclare @t table(seqno int identity原创 2006-05-11 13:53:00 · 1045 阅读 · 0 评论 -
筛选第m到n条记录
/*filter records with positions from M to N*/create table #test (val char(2))insert #test select a1insert #test select a2insert #test select a3insert #test select a4insert #test select a5ins原创 2006-05-10 12:39:00 · 1173 阅读 · 0 评论 -
如何不通过其他工具,把图片、声音等存储到sql中
用image类型方法:1、建立过程CREATE PROCEDURE sp_textcopy ( @srvname varchar (30), @login varchar (30), @password varchar (30), @dbname varchar (30), @tbname varchar (30), @coln转载 2006-04-15 09:37:00 · 873 阅读 · 0 评论 -
连接指定表的指定字符串字段的stored procedure
/*可惜function里面不能使用sp_executesql(除非自己写一个相同功能的extended procedure),要不写成function更为自然*/IF OBJECT_ID(strcat) IS NOT NULL DROP PROCEDURE strcatGOCREATE PROCEDURE strcat @cTable NVar原创 2005-12-31 12:04:00 · 953 阅读 · 0 评论 -
SQL Ranking 技巧
/*给一个table的某个字段排序,分别用sub-query和self-join。这个例子会有很多变化,例如qty值是否有重复,关键的关系运算符是 >, >=, */declare @t table(qty int)insert @t select 740insert @t select 100insert @t select 560insert @t原创 2005-12-28 10:12:00 · 1618 阅读 · 0 评论 -
quine (T-SQL Version, Excerpt From WWW)
print replace(0x2027202729,0x20,print replace(0x2027202729,0x20,)原创 2005-12-23 12:45:00 · 869 阅读 · 0 评论 -
用SQL将连续相同的字符压缩成一个字符
/* reduce the consecutive characters alike into single one WinXP, SQL Server 2K*/if object_id(fn_unicate) is not null drop function fn_unicategocreate function dbo.fn_unic原创 2005-12-21 14:51:00 · 1420 阅读 · 0 评论 -
sqlport : 用SQL Server檢測進程和端口的映射關係
set nocount ongocreate table #cmd_output (seqno int identity(1,1), value1 nvarchar(512))create table #tasklist(image_name varchar(30), pid int, session_name varchar(100), session_id varchar原创 2005-12-16 08:59:00 · 1209 阅读 · 0 评论 -
SQL server string feature(1)
-- SQL Server 2000declare @str nvarchar(10)set @str = + stringprint + @strprint + string原创 2006-05-11 14:05:00 · 1103 阅读 · 0 评论