sql 语句 收集

系统 1658 0

--查询表名包含某字符

Select *
From sysobjects
Where name like '%Area%' and type='u'

 

-- 含有'XXX'字段的所有表

select a.[name] TableName from sysobjects a,
(
select [id],count(*) b from syscolumns
where [name] ='SupplierREF'
group by [id]
)
b where a.[id]=b.[id]

 

---查看数据库表的约束

select a.name as DFName,b.Name as ColumnName
from sys.objects a inner join sys.columns b on a.object_id=b.default_object_id
where a.type='D'
and b.object_id=object_id(' 数据库表名 ')

 

---查看数据库表的外键

SELECT Table_Name as [Table], Column_Name as [Column],
Constraint_Name as [Constraint], Table_Schema as [Schema]
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
where Table_Name=' 数据库表名 '
ORDER BY [Table], [Column]

 

-----SqlServer如何查询表的列数

select count(*) from sysobjects a join syscolumns b
on a.id=b.id
where a.name='表名'

 

------ MSSQL查询所有没有主键的表

SELECT name FROM sys.tables
EXCEPT
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
 

---- SQL只复制表结构操作 (不如直接右键数据表使用Creaate脚本好用)

--复制表结构有句型的
--跨数据库
--复制结构+数据
select *  into 数据库名.dbo.新表名   from  数据库名.dbo.原表名
--只复制结构 
select *  into 数据库名.dbo.新表名   from  数据库名.dbo.原表名 where 1=0
--复制到临时表
select *  into #temptablename   from  数据库名.dbo.原表名 where 1=0 

--跨服务器数据库
select * into 本地库名.表名 from OPENDATASOURCE(
'SQLOLEDB',
'Data Source=远程ip;User ID=sa;Password=密码'
).库名.dbo.表名

 

---- SQL批量更新数据

UPDATE [Bms_Areas]
set ParentID =X.ParentID
,[AreaCode] = X.[AreaCode]
,[AreaName] = X.[AreaName]
,[Notes] =X.[Notes]
,[LevelNum] = X.[LevelNum]
,[IsDefault] = X.[IsDefault]
,[IsValid] = X.[IsValid]
,[IsDel] = X.[IsDel]
from [Bms_Areas] T, DGbell_erp.dbo.[Bms_Areas222] X
where T.AreaID =X.AreaID

 

-----实现删除表中某列有重复值的数据行

delete Bms_StoppageReason from Bms_StoppageReason a
where exists(select * from Bms_StoppageReason
where id<a.id and Code=a.Code)

 

----将一个表中某个字段的值全部更新到另外一个表相应的字段

update sp set sp.ParentID =sr.ID
from Bms_StoppagePhenomenon sp join Bms_StoppageReason sr
on sr.Code=sp.Descriptio

 

-----SQL分组取最新值
select 1,'A','2011-09-08 08:38:57.870',8,null union all
select 2,'A','2011-09-20 08:38:57.870',25,null union all
select 3,'A','2011-10-03 08:38:57.870',12,null union all
select 4,'B','2011-10-15 08:38:57.870',1,null union all
select 5,'B','2011-10-19 08:38:57.870',20,null union all
select 6,'B','2011-11-02 08:38:57.870',10,null union all
select 7,'C','2011-09-20 08:38:57.870',0,null union all
select 8,'C','2011-10-03 08:38:57.870',12,null union all
select 9,'C','2011-10-15 08:38:57.870',15,null

----------- ---- ----------------------- ----------- -----------
1 A 2011-09-08 08:38:57.870 8 NULL
2 A 2011-09-20 08:38:57.870 25 NULL
3 A 2011-10-03 08:38:57.870 12 NULL
4 B 2011-10-15 08:38:57.870 1 NULL
5 B 2011-10-19 08:38:57.870 20 NULL
6 B 2011-11-02 08:38:57.870 10 NULL
7 C 2011-09-20 08:38:57.870 0 NULL
8 C 2011-10-03 08:38:57.870 12 NULL
9 C 2011-10-15 08:38:57.870 15 NULL

所需结果如下:
----------- ---- ----------------------- ----------- -----------
3 A 2011-10-03 08:38:57.870 12 NULL
6 B 2011-11-02 08:38:57.870 10 NULL
9 C 2011-10-15 08:38:57.870 15 NULL

 

select col2,col4 from @t a
WHERE col3=(SELECT max(col3) FROM @t WHERE col2=a.col2)

 

----------- ---- ----------------------- ----------- -----------

drop table #Tmp --删除临时表#Tmp
create table #tmp
(
[TmpName] [varchar](50) NULL,
[TmpCount] [float] NULL
)
insert into #tmp
Select 'A', 320 union all
Select 'B', 21 union all
Select 'C', 54 union all
Select 'D', 236 union all
Select 'E', 325 union all
Select 'F', 32

 

SELECT a.[TmpName], a.[TmpCount] ,(a.[TmpCount]/b.T) as TmpPercent
FROM #tmp a, (SELECT SUM([TmpCount]) as T from #tmp )as b

 

-----------------------------sql中分组序号

例如:
111111 123
111111 345
222222 aaaa
222222 bbbb
222222 ccccc
变成: 111111 1 123
111111 2 345
222222 1 aaaa
222222 2 bbbb
222222 3 ccccc
每个小组有次序号。

----------》》

假设表名为 test1, 字段名为 a, b

SQL 语句如下:

select t.a, t.b, (SELECT COUNT(*) FROM test1 WHERE a = t.a AND b <= t.b) AS xuhao FROM test1 t ORDER BY t.a

 

 

 

----------------------------- 删除交集,我想在table1中删除table1和table2的交集

delete a
from table1 as a
inner join  
table2 as b on a.[field*]=b.[field*]

 

----------------按行号删除

delete T from (select *,row_number() over(order by ProductID,SHID) as RowIndex

from [Bms_Store_Current] )  T where T.RowIndex>2988

 

 

    假设将table1表的column1改名为column2,可以使用如下:
    
sp_rename 'table1.column1','column2','column'


SQL Server批量删除数据库表------------------------------------------

DECLARE @Table NVARCHAR(30)
DECLARE tmpCur CURSOR FOR
SELECT name FROM sys.objects WHERE TYPE='U' AND name LIKE N'Bms_%'
OPEN tmpCur
FETCH NEXT FROM tmpCur INTO @Table

WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @sql VARCHAR(100)
SELECT @sql = 'drop table ' + @Table
EXEC(@sql)
FETCH NEXT FROM tmpCur INTO @Table
END
CLOSE tmpCur
DEALLOCATE tmpCur

    
先找出你的外键所在用下面的语句:--查询一个表的所有外键:------------------------------------------

SELECT 主键列ID=b.rkey
,主键列名=(SELECT name FROM syscolumns WHERE colid=b.rkey AND id=b.rkeyid)
,外键表ID=b.fkeyid
,外键表名称=object_name(b.fkeyid)
,外键列ID=b.fkey
,外键列名=(SELECT name FROM syscolumns WHERE colid=b.fkey AND id=b.fkeyid)
,级联更新=ObjectProperty(a.id,'CnstIsUpdateCascade')
,级联删除=ObjectProperty(a.id,'CnstIsDeleteCascade')
FROM sysobjects a
join sysforeignkeys b on a.id=b.constid
join sysobjects c on a.parent_obj=c.id
where a.xtype='f' AND c.xtype='U'
and object_name(b.rkeyid) LIKE N'Bms_%'

 

-----------------比较两个数据库表的架构-------------------------

select name, schema_name(schema_id) [schema] from db1.sys.objects where type = 'U'
except
select name, schema_name(schema_id) [schema] from db2.sys.objects where type = 'U'

 

----------------- 相同username 找id小的 -------------------------

select a.* from ttt_test2 as a inner join
(
select user_name,min(id) as minid From ttt_test2 Group by user_name
) as b
on a.user_name=b.user_name and id=b.minid

 

-----------------使用SQL语句获得服务器名称和IP地址-------------------------

SELECT SERVERPROPERTY( 'MachineName' )
select @@SERVERNAME
select HOST_NAME()
 
SELECT SERVERNAME = CONVERT (NVARCHAR(128),SERVERPROPERTY( 'SERVERNAME' ))
,LOCAL_NET_ADDRESS AS 'IPAddressOfSQLServer'
,CLIENT_NET_ADDRESS AS 'ClientIPAddress'
  FROM SYS.DM_EXEC_CONNECTIONS WHERE SESSION_ID = @@SPID

sql 语句 收集


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论