Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

?select * from bdorder a join frmuser b on a.salesstaff = b.account and a.salesstaff like ‘asong%’
?select * from bdorder a join frmuser b on a.salesstaff = b.account and? b.account like ‘asong%’
bdorder 与 frmuser有相同的用户,在where条件中选择b.account进行筛选比用bdorder的salesstaff要好的多,因为不可避免的是bdorder有重复的salesstaff,而frmuser则不会有重复,就少了一些比较。
即:在使用join时,where条件选择数据较少的表的字段进行筛选。


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

 --检查sqlserver所在服务的运行账号是否有权限访问共享文件夹,没有的话右键添加写权限
 --检查方法:建立一网络驱动器,右键属性安全
 --注:若sqlserver在localsystem账号下面运行,是访问不了共享文件夹的,要改成有权限的账号,如network services

 --开启权限
sp_configure 'show advanced options', 1;
go
reconfigure;
go

 EXEC sp_configure 'xp_cmdshell', 1

GO

reconfigure;
go

DECLARE @DesAddress NVARCHAR(500),@DesAccount NVARCHAR(100),@DesPassword NVARCHAR(100), @DataBaseName VARCHAR(100)
SET @DesAddress = '\\172.16.88.204\soft-0509\aa'
SET @DesAccount = 'administrator'
SET @DesPassword = 'Lvshou!@#'
SET @DataBaseName = 'master'

DECLARE @bakName VARCHAR(500)
SET @bakName = @DataBaseName + '_' +  convert(varchar(10),getdate(),112) + '.bak'
SET @bakName = @DesAddress + '\' + @bakName
--print @bakName

DECLARE @sql NVARCHAR(1000)

--连接共享,打开通道
SET @sql = 'net use Y: '+ @DesAddress+ ' ' + @DesPassword + ' ' +'/user:'+@DesAccount
PRINT @sql
EXEC master..xp_cmdshell @sql

--备份
SET @sql = 'backup database ' +@DataBaseName + ' TO DISK = ' + QUOTENAME(@bakName,'''')
--PRINT @sql
EXEC (@sql)

--删除一周前备份
SET @sql = 'del ' + @DesAddress + '\' + @DataBaseName + '_' +  convert(varchar(10),dateadd(day,-7,getdate()),112) + '.bak'
--PRINT @sql
EXEC master..xp_cmdshell @sql

--执行关闭关掉通道
exec master..xp_cmdshell 'net use Y: /delete /y' 

--关闭权限
EXEC sp_configure 'xp_cmdshell', 0

GO

reconfigure;
go


 sp_configure 'show advanced options', 0
go
reconfigure;
go


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

1,nchar、nvarchar则最多存储4000个字符,不论是英文还是汉字;
而char、varchar最多能存储8000个英文,4000个汉字。
2,NCHAR、 NVARCHAR、NTEXT。这三种从名字上看比前面三种多了个“N”。它表示存储的是Unicode数据类型的字符。我们知道字符中,英文字符只需要一个字节存储就足够了,但汉字众多,需要两个字节存储,英文与汉字同时存在时容易造成混乱,Unicode字符集就是为了解决字符集这种不兼容的问题而产生的,它所有的字符都用两个字节表示,即英文字符也是用两个字节表示。nchar、nvarchar的长度是在1到4000之间。和char、 varchar比较起来,
即:varchar一个字节存一个字母,而nvarchar需要有两个,故在8000个存储空间下,nvarchar最大可以存4000个。


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

sp_lock
dbcc inputbuffer(53)
kill 53
SQL SERVER Profiler

Microsoft SQL Server 2005 提供了一些工具来监控数据库。方法之一是动态管理视图。动态管理视图 (DMV) 和动态管理函数 (DMF) 返回的服务器状态信息可用于监控服务器实例的运行状况、诊断问题和优化性能。

常规服务器动态管理对象包括:

dm_db_*:数据库和数据库对象

dm_exec_*:执行用户代码和关联的连接

dm_os_*:内存、锁定和时间安排

dm_tran_*:事务和隔离

dm_io_*:网络和磁盘的输入/输出

此部分介绍为监控 SQL Server 运行状况而针对这些动态管理视图和函数运行的一些常用查询。


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
declare @salesstaff varchar(50)
set @salesstaff = null
SELECT @SalesStaff = SalesStaff FROM bdCustomerAllocate WHERE Departmentid = 4 AND CustomerID = -111
if ( @salesstaff  is   null )
begin
    print 'null'
end
else
begin
    print 'sdf'
end

若数据库中没有此记录,则不会给 @SalesStaff赋值,它会保留原值,故最好在赋值前将其设为NULL


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

(1)select * from bdorder
(2) select * from bdorder
order by orderdate desc
前者执行为15% 而后者要85% 差了5倍多!
故在用DESC 要慎用,效率太低!

典型应用:
左连接取出连接表符合条件的第一条记录
left? join bdship d(NOLOCK)
on d.orderno = c.orderno
AND d.id=(select top 1 id from bdship m where m.orderno = d.orderno order by id desc )
应改为:
left? join bdship d(NOLOCK)
on d.orderno = c.orderno
AND d.shipdate=(select max(shipdate) from bdship m where m.orderno = d.orderno? )


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
 CREATE                PROCEDURE [dbo].[bdCustomerAllocate_sel]
@TranType varchar(40) = NULL,
@Id int = NULL,
@CustomerID int = NULL,
@OpenDate varchar(20) = NULL,       //在 AS 之前的是形参,则程序中传值,程序中传值个数可少于形参个数,前提是没有被传的形参要赋值,如NULL

AS
DECLARE @CustoemrName varchar(20)        //在As之后用 Declare定义变量,只在存储过程内部使用,不是形参。
SET @CustomerName = ''
IF(@TranType = 'personalcustomerlist')
BEGIN
    SELECT a.id, b.CustomerName, b.Address, b.Tel, 
    (SELECT TOP 1 FollowDate FROM bdFollowRecord T(nolock)
        WHERE T.AllocateID = a.ID) AS FollowDate
    FROM  bdCustomerAllocate a(NOLOCK)
    JOIN bmdCustomer b(NOLOCK)
    ON a.CustomerID = b.ID    
    WHERE a.IsValid = 1
END


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
update bmdcustomer set customerid = 1

若想连接其它的表
update a set customerid = 1 
from bmdcustomer a
join ....
delete也一样


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

采用字符串式:
user: sa’ or ‘1=1 或 sdfsdfsd’ or ‘1=1 — (无论用户名密码是否正确都可进入。注:mysql注释用#,sqlserver用–)
password: sdfsdf ( 随便 )
用字符串连接就变成: select * from frmUser where user = ‘sa’ or ‘1=1’ and password = ‘sdfsdf’ 通过!造成注入。
万能密码:’or”=”or”=’
采用传参方式:
command.CommandText = “SELECT * FROM frmUser WHERE user = @user”;
SqlParameter sp = new SqlParameter( “@user” ,8);
若传user = sa’ or ‘1=1
则sql 语句变成: select * from frmUser where user = “sa’ or ‘1=1’ ” and and password = ‘sdfsdf’
将user传的参数限定在一个变量内。原理是在参数外加上””, 之后若参数内存在单引号或双引号,则统一转成单引号,因最外部是双引号,参数中的单引号,双引号将会被当作用户名的一部分,不会当成分隔,不会象字符串连接一样使sql语句被截断,产生多个条件!


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

GO : 用户定义变量的作用域限制在一个批处理中,不可在 GO 命令后引用。即GO将语句分成一个个函数,隔绝彼此。


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

1,用EXISTS替代IN、用NOT EXISTS替代NOT IN;
in的逻辑和 or是相同的,查询时必须找到所有符合的记录。

但exists则不同,他是存在的逻辑,也就是找到有一个就可以了
SELECT * FROM bdOrder
WHERE customid in ( select id form bmdCustomer ) //子查询会检查每一条记录

SELECT * FROM bdOrder a
WHERE EXISTS
( SELECT 1 FROM bmdCustomer b WHERE b.id = a.customerid ) //检查到存在的记录就返回


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

declare @OrderNo varchar(20)
set @OrderNo = ‘123’ + null

print @OrderNo

结果为:NULL 什么也没有
即: 字符串 + NULL = NULL (此点要注意!)
换用 set @OrderNo = ‘123’ + ISNULL(@SDF, ”)

NULL 与任何操作都是NULL ,无论是 加法还是逻辑运算


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

DBO是每个数据库的默认用户,具有所有者权限,即DbOwner,在创建表时没有指定所有者,那么系统默认该表的所有者是dbo,
它代表共享,若建表时把所有者指给了Dbo,则别的用户进来时写上Dbo.Table就可访问。


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

不加@代表要声明的是cursor, 或者系统关键字SELECT,
系统变量一般为@@identity


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
注意的一点时加个newid字段,以记录使每一次导入都不同,可恢复之类的操作


DECLARE @nid varchar(50)
DECLARE @dt  datetime


SET @nid = newid()
SET @dt = getdate()

INSERT INTO CustomerTemp


SELECT   'zhuyunbo' AS Developer, 销售部门 AS DepartmentName, 日期 AS ReceiveDate,
工号 AS WorkNo, 顾问姓名 AS SalesStaffName, 客户姓名 AS CustomerName, 地址 AS Address,
联系电话 AS Tel, 性别 AS Sex, 身高体重年龄 AS Height_Weight_Age,
客户状态 AS CustomerStatus, 沟通记录  AS Record1,
F12  AS Record2,
NULL AS Record3,
@dt AS ImportDate,
@nid As nd
FROM   OpenDataSource( 'Microsoft.Jet.OLEDB.4.0', 'Data Source= "G:\发展部数据资料\发展部祝云波资料.xls";Extended Properties= "Excel 5.0;HDR=YES;Excel 8.0";Persist Security Info=False ')...[客户资料库$]

print @nid

print @dt


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
SELECT   *  into newtable 
FROM   OpenDataSource( 'Microsoft.Jet.OLEDB.4.0', 'Data Source= "G:\Share\st.xls";Extended Properties= "Excel 5.0;HDR=YES;Excel 8.0";Persist Security Info=False ')...[客房库资料$]

注:客房库资料$ 为Excel的一个sheet
Excel及运行的数据库要在同一台电脑上。G:\Share\st.xls 为服务器上文件路径

注: Excel列名若不存在,默认为F2之类

HDR=Yes/No
可选参数,指定 Excel 表的第一行是否列名,缺省为 Yes,可以在注册表中修改缺省的行为。

IMEX=1 混合数据类型列的强制解析
使用 IMEX=1 选参之后,只要取样数据里是混合数据类型的列,
一律强制解析为 nvarchar/ntext 文本。当然,IMEX=1 对单一数据类型列的解析是不影响的。
可使用此参考解决科学计数法问题

参考:http://blog.csdn.net/htl258/article/details/5450497


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

?查询分析器,右键调试

为客户端打开调试功能:master数据库–扩展存储过程–sp_sdidebug 给予权限


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

?SQL SERVER
bigint

从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字)。存储大小为 8 个字节。

int

从 -2^31 (-2,147,483,648) 到 2^31 – 1 (2,147,483,647) 的整型数据(所有数字)。存储大小为 4 个字节。int 的 SQL-92 同义字为 integer

smallint

从 -2^15 (-32,768) 到 2^15 – 1 (32,767) 的整型数据。存储大小为 2 个字节。

tinyint

从 0 到 255 的整型数据。存储大小为 1 字节。

C#中:?2,147,483,648 to 2,147,483,647? 同SqlServer的int (同java )

char?? 0 – 255

C 中,用两个字节表示: -2^15 (-32,768) 到 2^15 – 1 (32,767)


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

?从程序中构造sql语句执行时可用,防止多重判断
?sb.Append( ” AND( 1=2 ” );
??????????? if (chkFilter.Items[0].Selected)
??????????? {
??????????????? sb.Append(” OR ADSource=””);
??????????? }
??????????? if (chkFilter.Items[1].Selected)
??????????? {
??????????????? sb.Append(” OR SearchKey=””);
??????????? }
??????????? if (chkFilter.Items[2].Selected)
??????????? {
??????????????? sb.Append(” OR WebSource=””);
??????????? }
??????????? sb.Append( ” )” );


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
注用 join后, 条件要写在之后
select * from bdCustomerAllocate a
left join bdFollowRecord b
on ( a.id = b.allocateid and b.id = (select top 1 id  from bdFollowRecord where allocateid = a.id order by followdate desc ))
where a.SalesStaff = 'liuhong'
而不要用:
select * from bdCustomerAllocate a
left join bdFollowRecord b
on a.id = b.allocateid 
where a.SalesStaff = 'liuhong'
and b.id = (select top 1 id  from bdFollowRecord where allocateid = a.id order by followdate desc )
保证where 句中的条件都是from 后的表,而不是join的表

 
1,条件写在left join, right join集合内。
(作left join得到的join 集合一般是统计数据,故与统计相关的条件都要放在left join之内)

(1)  SELECT DISTINCT c.account FROM frmUser c
(2)  LEFT JOIN bdCustomerAllocate a
(3)  ON c.Account = a.SalesStaff
(4)  WHERE a.SalesStaff in ( SELECT ControlAccount FROM permUserRelation WHERE CurrentAccount = 'luni' )

若执行前三步,则结果是以frmUser为主的数据集,若全部执行,得到的结果好象以bdCustomerAllocate为主,没有达到左连接的效果。原因:分清where的作用,前三步得到的数据集,在where中会将不满足条件的除去,因是以左连接bdCustomerAllocater的SalesStaff作判断,故结果集将以bdCustomerAllocater为主,没有达到左连接效果。

故在写 left join 时,join 的数据集最好执行去掉全部条件,而不是在left join之后再用where去除。
SELECT DISTINCT c.account FROM frmUser c
LEFT JOIN ( SELECT SalesStaff FROM bdCustomerAllocate WHERE SalesStaff in ( SELECT ControlAccount FROM permUserRelation WHERE CurrentAccount = 'luni' ) ) a
ON c.Account = a.SalesStaff

2,子集合后要有另名:
SELECT * FROM ( SELECT * FROM bdCustomerAllocate ) a  //无别名a是错误的,因为不指定别名,怎么在条件中引用数据集中的字段。

3, from 后的 join是对表集合的一种补充

SELECT DISTINCT c.account,d.* FROM frmUser c
JOIN bdCustomerAllocate a
ON c.Account = a.SalesStaff
, bdCustomerAllocate d

join相当于对from后的表对一种补充,之后再用逗号引用其它表。下面的是错误的,因为join作用在紧跟它的表后面,此时会作用在d上,d没有account
SELECT DISTINCT c.account,d.* FROM frmUser c, bdCustomerAllocate d
JOIN bdCustomerAllocate a
ON c.Account = a.SalesStaff