1、删除某字段中的重复记录,Table1006表中有Id和Phone字段,Id为不重复的标字段,但是Phone含有重复字段,现在需要重复Phone记录行删除掉,只保留最新一条记录。
if OBJECT_ID('dbo.Table1006') is not null
drop table dbo.Table1006
create table dbo.Table1006
(
[Id] int,
[Phone] varchar(10)
)
insert into dbo.Table1006 values(1, 100)
insert into dbo.Table1006 values(2, 110)
insert into dbo.Table1006 values(3, 120)
insert into dbo.Table1006 values(4, 130)
insert into dbo.Table1006 values(5, 110)
insert into dbo.Table1006 values(6, 110);
insert into dbo.Table1006 values(7, 120);
delete from dbo.Table1006
where [Id] not in ( select max([Id]) as [Id]
from dbo.Table1006
group by [Phone])
select * from dbo.Table1006
drop table dbo.Table1006
2、 生成表结构(可贴到Excel)方便编辑。
T-SQL
-- 生成数据库表结构说明
DECLARE TableCrsr CURSOR FOR
SELECT [id], [name]
FROM [sysobjects]
WHERE ([type] = 'U')
ORDER BY [name]
OPEN TableCrsr;
DECLARE @tid int
DECLARE @tname nvarchar(max)
DECLARE @tdesc nvarchar(max)
FETCH NEXT FROM TableCrsr INTO @tid, @tname
WHILE (@@FETCH_STATUS <> -1)
BEGIN
SET @tdesc = ''
SELECT @tdesc = CAST([value] AS nvarchar(max)) FROM ::fn_listextendedproperty(default, 'USER', 'dbo', 'TABLE', @tname, default, default)
PRINT '-- ' + @tname + ':' + @tdesc + ' --'
DECLARE ColumnCrsr CURSOR FOR
SELECT [syscolumns].[name], [systypes].[name], [syscolumns].[prec], [indexesandcolumns].[is_primary_key], CAST([extendedproperty].[value] AS nvarchar(max))
FROM [syscolumns]
JOIN [systypes] ON ([syscolumns].[xusertype] = [systypes].[xusertype])
LEFT JOIN ::fn_listextendedproperty(default, 'USER', 'dbo', 'TABLE', @tname, 'COLUMN', default) AS [extendedproperty] ON ([syscolumns].[name] = [extendedproperty].[objname] COLLATE Chinese_PRC_CI_AS)
LEFT JOIN (
SELECT [column_id], [is_primary_key] FROM sys.index_columns AS [indexcolumns]
LEFT JOIN sys.indexes AS [indexes] ON ([indexes].[object_id] = @tid) AND ([is_primary_key] = 1)
WHERE ([indexcolumns].[object_id] = @tid) AND ([indexcolumns].[index_id] = [indexes].[index_id])
) AS [indexesandcolumns] ON ([indexesandcolumns].[column_id] = [syscolumns].[colid])
WHERE ([id] = @tid)
OPEN ColumnCrsr;
DECLARE @cname nvarchar(max)
DECLARE @ctype nvarchar(max)
DECLARE @cprec int
DECLARE @primarykey bit
DECLARE @cdesc nvarchar(max)
FETCH NEXT FROM ColumnCrsr INTO @cname, @ctype, @cprec, @primarykey, @cdesc
WHILE (@@FETCH_STATUS <> -1)
BEGIN
PRINT @cname + ' ' + @ctype + ' ' + CAST(@cprec AS nvarchar(max)) + ' ' + ISNULL(CAST(@primarykey AS nvarchar(max)), '') + ' ' + ISNULL(@cdesc, '')
FETCH NEXT FROM ColumnCrsr INTO @cname, @ctype, @cprec, @primarykey, @cdesc
END
CLOSE ColumnCrsr;
DEALLOCATE ColumnCrsr;
PRINT ''
FETCH NEXT FROM TableCrsr INTO @tid, @tname
END
CLOSE TableCrsr;
DEALLOCATE TableCrsr;
运行SQL语句,然后把Messages贴到Excel中就能方便的对结构进行编辑了。
3、字符串特性。
字符串具有恒定性、字符串驻留(跨AppDomain)、参数传递的特殊性。
Code
static void Main(string[] args)
{
string strA = "abcdef";
string strB = "abcdef";
Console.WriteLine(ReferenceEquals(strA, strB));//字符串的恒定性
string strC = "abc";
string strD = strC + "def";
Console.WriteLine(ReferenceEquals(strA, strD));//对于动态生成的字符串,字符串驻留机制失效
strD = String.Intern(strD);
Console.WriteLine(ReferenceEquals(strA, strD));//使用Intern静态方法,能强制把字符串添加到哈希表中,并返回引用,便可以使字符串驻留。
string strE = "abc" + "def";
Console.WriteLine(ReferenceEquals(strA, strE));//使用+号,编译器会自动将其连接为一个文本常量加载,因此会添加到内部哈希表中。
//不同的线程、进程、AppDomain中字符串驻留都有效
System.Threading.Thread thread = new System.Threading.Thread(delegate(object str)
{
string strF = "abc" + "def";
Console.WriteLine(ReferenceEquals(str, strF));
});
thread.Start(strA);
thread.Join();
string strG = strC + "def";
System.Threading.Thread thread2 = new System.Threading.Thread(delegate(object str)
{
string strF = "abc" + "def";
Console.WriteLine(ReferenceEquals(str, strF));
});
thread2.Start(strG);
thread2.Join();
Console.ReadLine();
//result:
//True
//False
//True
//True
//True
//False
}
4、statistics io
set statistics io on
set statistics io off