























-- group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。 delete table tableName where id not in ( select max(id) from table group by col1,col2,col3... )
select * into #temp from tablename group by id1,id2,.... delete tablename insert into table select * from #temp drop table #temp
select distinct * into #temp from tablename delete tablename go insert tablename select * from #temp go drop table #temp
select identity(int,1,1) as id,* into #temp from tabel delect # where id not in ( select max(id) from # group by col1,col2,col3...) delect table inset into table(...) select ..... from #temp
select * from table where col1+','+col2+','...col5 in ( select max(col1+','+col2+','...col5) from table where having count(*)>1 group by col1,col2,col3,col4 ) -- group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。
select identity(int,1,1) as id,* into #temp from tabel select * from #temp where id in ( select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)
CREATE FUNCTION dbo.splitstring
(
@stringToSplit VARCHAR(MAX)
)
RETURNS @returnList TABLE ( [Name] [nvarchar](500) )
AS
BEGIN
DECLARE @name NVARCHAR(255)
DECLARE @pos INT
WHILE CHARINDEX(',', @stringToSplit) > 0
BEGIN
SELECT @pos = CHARINDEX(',', @stringToSplit)
SELECT @name = SUBSTRING(@stringToSplit, 1, @pos - 1)
INSERT INTO @returnList
SELECT @name
SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos + 1,
LEN(@stringToSplit) - @pos)
END
INSERT INTO @returnList
SELECT @stringToSplit
RETURN
END
CREATE FUNCTION [dbo].[ufnGetSplitValues]
(
@StringValues NVARCHAR(4000),
@Delimiter CHAR(1)
)
RETURNS TABLE
AS
RETURN
(
SELECT LTRIM(RTRIM(Split.a.value('.', 'NVARCHAR(100)'))) 'Value'
FROM
(
SELECT CAST ('<M>' + REPLACE(@StringValues, @Delimiter, '</M><M>') + '</M>' AS XML) AS Data
) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
)
GO
SELECT * FROM STRING_SPLIT('a,b,cd', ',');
/* result:
value
--------
a
b
cd
*/
USE Database1 GO -- 如果不加SET FMTONLY OFF,在执行时可能会出现分布式事务异常 SELECT name, age, sex INTO #Persons FROM OPENQUERY(LinkedServer, 'SET FMTONLY OFF EXEC [database2].[dbo].uspGetPersonList')
DECLARE @Sql VARCHAR(1000) DECLARE @organizationID VARCHAR(10) SELECT @organizationID = (SELECT ORGANIZATION_ID FROM MYORGS WHERE ORGANIZATION_NAME = 'MMT') SET @Sql = 'SELECT * from tableName where organization_id ='+@organizationID SET @Sql = 'SELECT * FROM OPENQUERY(LinkedServerName, ''' + REPLACE(@Sql, '''', '''''') + ''')' EXEC(@Sql)
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。