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


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,客户端连接服务器
在新建注册的时候,可连两个,一个是服务器的别名(局域网使用),一个是IP指定。一般远程的通过IP指定来完成。若想连接多个,在客户端网络实用工具:别名–添加–指定服务器别名,选TCP/IP指定服务器IP,之后注册时就可通过此别名来连接服务器。
2,服务器建立注册也可建立两个,一个是localhost(同本机名),一个是IP,不过都相当于同一个。Sql Server服务管理器服务器可注册的任一个即可,因为都是同一个。即:服务端只能对外提供一个实例(除非装两个Sql Server), 在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   @i   int
set   @i=1
select top @i, * from table1
在存储过程中得不到执行,因为top时后面只能是常量。

可用:
 declare   @i   int
set   @i=1
set  rowcount @i
select * from table1
set rowcount = 0

rowcount:指定查询返回的记录条数,使命令的处理在响应指定的行数之后停止处理命令
set  rowcount @i
select * from table1  相当于 select top 1 from table1
set rowcount = 0 取消指定


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 @@version

2,查看数据库所在机器操作系统参数
exec master..xp_msver
查看数据库启动的参数
sp_configure
查看数据库启动时间
select convert(varchar(30),login_time,120) from master..sysprocesses where spid=1
查看所有数据库名称及大小
sp_helpdb
查看所有数据库用户登录信息
sp_helplogins
3,.查看某数据库下某个数据对象的大小
sp_spaceused @objname

4,查看数据库里所有的存储过程和函数
use @database_name
sp_stored_procedures

sp_help 查看表结构, 存储过程实参

5,查看存储过程和函数的源代码
sp_helptext ‘@procedure_name’

或者OBJECT_DEFINITION(object_id(‘sys.sysobjects’))
或者select * from sys.system_sql_modules
where object_id = object_id(‘sys.sysobjects’)

6,查看数据库里用户和进程的信息
sp_who
查看SQL Server数据库里的活动用户和进程的信息
sp_who ‘active’
看进程正在执行的SQL语句:
dbcc inputbuffer (51) 51为进程号

7,查看SQL Server数据库里的锁的情况
sp_lock
进程号1–50是SQL Server系统内部用的,进程号大于50的才是用户的连接进程。spid是进程编号,dbid是数据库编号,objid是数据对象编号


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,@@SERVERNAME 返回运行 Microsoft? SQL Server? 的本地服务器名称。

2,@@SERVICENAME 返回 Microsoft? SQL Server? 正在其下运行的注册表键名,SQL Server 作为名为 MSSQLServer 的服务在 Microsoft Windows NT? 上运行

3,
?HOST_ID() –主机编号
?HOST_NAME()–主机名
?DB_ID(‘master’) –数据库编号
?DB_NAME(1)????? –数据库名称
?OBJECT_ID(‘sysobjects’)? –数据库对象编号
?File_ID(‘master’)??????? –返回该逻辑文件名的编号
?File_NAME(1)????????????? –返回该文件编号的逻辑文件名
?FILEGROUP_ID(‘primary’)???? –返回文件组编号
?FILEGROUP_NAME(1)?????????? –返回文件组名称

4, COL_NAME(OBJECT_ID(‘sysobjects’),1)? –表中列的名称,每个表中列是从…n顺序编号
?????? INDEX_COL(‘sysobjects’,1,1)?? –返回sysobjects中第一个索引第一个键的名称
?????? APP_NAME() –返回当前使用的工具名称,如:SQL 查询分析器
? ? ? @@SPID????? –返回当前连接在SQL SERVER中的编号
?? ?? @@PROCID??? –在SP中使用,查看自己的ID
????? Suser_Sid()????? –返回登录的SID,可传参,默认返回当前值
????? Suser_Sname()??? –返回登录的名称,可传参,默认返回当前值
????? System_User????? –等价于不带参数的SUER_SNAME()
????? User_Id()????????? –返回数据库用户ID,可传参,默认返回当前值
?? ?? User_Name()?????? –返回数据库用户名,可传参,默认返回当前值
? ? ? User??????????????? –等价于不带参数的USER_NAME()
?? ?? Current_User????? –等价于不带参数的USER_NAME()
????? Session_User????? –等价于不带参数的USER_NAME()

5,
获取长度
DATALENGTH(‘s我s’) –返回表达式占用的字节数
COL_LENGTH(‘sysobjects’,’name’) –返回列的定义长度(byte)
日期时间转换为字符串

6,
日期时间转换为字符串
convert(varchar(20),getdate(),120)

7,@@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

系统表的作用:用户自定义的表对自己填充的数据进行操作。而对sql server来说,用户自定义的表就相当于为系统表填充的数据,故用系统表来操作用户数据库,
数据表,存储过程等数据。 系统表操作用户自定义表,用户自定义表操作外来的数据。

1,sysobjects表:万物皆对象,这是一个系统对象表。表,存储过程,触发器都是对象,都存在此表内。此表用crdate 等保存 对象的创建日期。
查找用户表: select *?? from sysobjects where xtype = ‘U’ AND status >0
??????? 因dtproperties虽是系统表,但类型为’U’, 但status为负数,故得到纯净的用户表时要将其除去。dtproperties这个表里保存的是关系图
查存储过程: select *?? from sysobjects where xtype = ‘P’
注:Type是在SQL SERVER 6.0就有的,xType在SQL SERVER 7.0才出现,Type的保留只是为了向后兼容。
?????
?????? 相关函数:object_id(‘mytable’),查出对象名在sysobjects表中的id值
??????????????????????? object_id ( object_id ), 据id查出对象名

2,sys.servers 查看所有本地服务器及链接服务器 select * from master..sysservers 该表只存储在 master 数据库中。

3,sys.databases 查询非sa创建的所有数据库 select * from master..sysdatabases 该表只存储在 master 数据库中。

4, sys.columns 获取表或视图的所有字段,存储过程或函数的所有参数 select name from syscolumns where id=object_id(‘表名’)

5,sys.indexes 数据库中的每个索引。

6, sys.logins? 每个登录帐户在表中占一行。

7,?? sys.processes 数据库进程信息 ,只存储在 master 数据库中。
?? 查看用户进程信息 select spid,uid,syslogins.name,login_time,net_address from sysprocesses,syslogins where sys.processes.sid=syslogins.sid

8,sysdepends? 查看与某一个表相关的视图、存储过程、函数? select * from sysdepends where depid=object_id(‘表名’)

9,sysmessages SQL SERVER返回的内部错误都有在这里,可自行修改,当错误发生时显示自己指示的信息。

10,sysfiles 保存当前数据库所在文件大小,路径等信息。

11,INFORMATION_SCHEMA 当前数据库的每个 CHECK 约束在该视图中占一行。该信息架构视图返回当前用户对其拥有权限的对象的有关信息。
?????? 查询某个表的哪些字段不允许为空
??????????? select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where IS_NULLABLE=’NO’ and TABLE_NAME=’bdorder’


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、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
法一:select * into b from a where 1 <>1
法二:select top 0 * into b from a 

        同用户下别一数据库表的引用为 mydb..mytable   因表与字段之间用一个点表示,故为了区别,数据库与表之间的联系用两个点

2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
insert into b(a, b, c) select d,e,f from b; 

3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件
例子:..
from b in '"&Server.MapPath(".")&"\data.mdb" &"' where.. 

4,说明:日程安排提前五分钟提醒
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5 

5,说明:随机取出10条数据
select top 10 * from tablename order by newid() 

6、说明:随机选择记录
select newid() 

7,说明:删除重复记录
Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
id 为主键,主键一定不会重复,重复的只是col1,col2 ,按col1,col2分组,相同的分到一组,此时取出最大的id,(min也一样)剩下的就是重复的,去掉即可。
注:dinstinct只能区别一个列值,若想得到唯一的一行记录,可用:
select from tablename where id in( select max(id) from tablename group by col1,col2,... )
select max(id) from tablename group by col1,col2,...  取出表中唯一的行,那么剔除重复,唯一行的个数为:
select count(1) from (select max(id) as id from tablename group by col1,col2,...) m
也可用:select count(1) from (select distinct col1,col2,... from tablename ) m

8、说明:列出数据库里所有的表名
select name from sysobjects where type='U' 

 9,说明:选择从10到15的记录
select top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc
N到M条记录(要有主索引ID)
Select Top M-N * From 表 Where ID in (Select Top M ID From 表) Order by ID  Desc  

10,按姓氏笔画排序:
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as 

11,查看当前数据库中所有存储过程
select name as 存储过程名称 from sysobjects where xtype='P' 

 12,一条语句执行跨越若干个数据库
select * from OPENDATASOURCE('SQLOLEDB','Data Source=远程ip;User ID=sa;Password=密码').库名.dbo.表名 

13 数据库里有1,2,3,4,5 共5条记录,要用一条sql语句让其排序,使它排列成4,5,1,2,3,怎么写?

select * from t
 order by case id when 4 then 1
                  when 5 then 2
                  when 1 then 3
                  when 2 then 4
                  when 3 then 5 end 

 或:
select * from t order by charindex(cast(id as varchar),'45123')
14,取出一个年级各班的前两名:
class? name? score
2班??? 张二??? 400
2班??? 张三??? 300
2班??? 张四??? 200
2班??? 张五??? 100
3班??? 刘一??? 500
3班??? 刘二??? 400
3班??? 刘三??? 300
3班??? 刘四??? 200
3班??? 刘五??? 100

SELECT t.class,t.name,t.socre
FROM tmp t
WHERE EXISTS( SELECT 1 FROM? (SELECT TOP 2 * FROM tmp tm WHERE tm.class = t.class ORDER BY tm.socre DESC )m WHERE? m.name = t.name )
ORDER BY t.class,t.socre DESC,t.name
--利用子查询获取一个班级内最高的两名然后与总表连接