Microsoft SQL Server code snippets collection

MS SQL collection snippet

Query dependencies

SELECT DISTINCT object_Name(id) FROM syscomments
WHERE id IN (SELECT id FROM sysobjects WHERE type IN ('P'))
AND text LIKE '%table_name%'

The query contains this table

select definition from sys.sql_modules where definition like '%table_name%'

View Index

SELECT * FROM sys.indexes WHERE object_id=OBJECT_ID('table_name', N'U')

Check Constraints

sp_helpconstraint 'table_name'

Removing Constraints

alter table [table_name] drop constraint [constraint_name]

/*  通过存储过程查询一个表的所有索引
sp_helpindex [ @objname = ] 'name'
参数 :[@objname =] 'name'  是当前数据库中表或视图的名称。name 的数据类型为 nvarchar(776),没有默认值。 */
sp_helpindex 'table_name'

Query system table indexes and index columns

SELECT  indexname = a.name , tablename = c. name , indexcolumns = d.name , a.indid
FROM  sysindexes a JOIN sysindexkeys b ON a.id = b.id  AND a.indid = b.indid
  JOIN sysobjects c ON b.id = c.id
  JOIN syscolumns d ON b.id = d.id  AND b.colid = d.colid
WHERE  a.indid NOT IN ( 0 , 255 )
and  c.xtype='U' AND c.name = 'table_name' and  c.status>0
ORDER BY c. name ,a.name ,d.name

Delete Index

DROP INDEX [index_name] ON [table_name]

View statistics

sp_autostats 'table_name'

Delete statistics

DROP STATISTICS [index_name]

Just take the numbers

SELECT CASE WHEN patIndex('%[^0-9]%',@S)>0 THEN left(@S,patIndex('%[^0-9]%',@S)-1) ELSE 0 END

Cross-database data guide

exec sp_configure 'Ad Hoc Distributed Queries' , [0 or 1]  --用时开启,用完关闭
reconfigure
exec sp_configure 'show advanced options' , [0 or 1]
reconfigure SELECT * FROM  openrowset  ( 'SQLOLEDB' , '[servername]' ; '[username]' ; '[password]' , [databasename].[dbo].[tablename] )

Database full-width half-width characters turn

CREATE FUNCTION f_Convert
(
  @str NVARCHAR(4000), --要转换的字符串
  @flag bit --转换标志,0转换成半角,1转换成全角
)RETURNS nvarchar(4000)
AS
BEGIN
DECLARE @pat nvarchar(8),@step int,@i int,@spc int
IF @flag=0
SELECT @pat=N'%[!-~]%',@step=-65248,
@str=REPLACE(@str,N'  ',N' ')
ELSE
SELECT @pat=N'%[!-~]%',@step=65248,
@str=REPLACE(@str,N' ',N'  ')
SET @i=PATINDEX(@pat COLLATE LATIN1_GENERAL_BIN,@str)
WHILE @i> 0
SELECT @str=REPLACE(@str,
SUBSTRING(@str,@i,1),
NCHAR(UNICODE(SUBSTRING(@str,@i,1))+@step))
,@i=PATINDEX(@pat COLLATE LATIN1_GENERAL_BIN,@str)
RETURN(@str)
END
GO
//调用:
UPDATE [tablename] set brnl=dbo.f_Convert(brnl,0)

Implement random query (over function and newid)

over the function and row_numbert sql () function with the use of the line number may be generated. Sort the values of a column, sort the data line grouping the same value.
NEWID () to create a unique value uniqueidentifier type, style like this 6F9619FF-8B86-D011-B42D-00C04FC964FF
because a select whenever the scanned record call this function each time, so that the same value is generated from each record each result, if the sort (order according to this value by NEWID ( )), it produces the effect similar to random recording.

select row_number() over(order by AID DESC) as rowid,* from bb

Guess you like

Origin www.cnblogs.com/viazure/p/12208531.html