我们有一个网络应用程序,我们想向潜在客户演示,但我们最好的方法是使用现有数据,以获得完整的体验。当然,我们不希望在应用程序中看到实际的客户名称或地址等。在SQL Server中有一种简单的方法可以随机化或加扰varchar或文本字段吗?
无论如何,这些列都不是主键或外键。
答案 0 :(得分:5)
这是一个迟到的答案,但我对此事的任何互联网搜索都不满意。这是一个示例,它将洗牌客户表中的名字和姓氏以创建新名称:
--Replace Customers with your table name
select * from Customers
--Be sure int match your id column datatype
Declare @id int
--Add a WHERE here to select just a subset of your table
DECLARE mycursor CURSOR FOR SELECT id FROM Customers
OPEN mycursor
FETCH NEXT FROM mycursor INTO @id;
WHILE (@@FETCH_STATUS = 0)
BEGIN
--We loop
--Warning: NEWID() is generated once per query, so we update the fullname in two queries.
UPDATE Customers
SET FirstName = (SELECT TOP 1 FirstName FROM Customers ORDER BY NEWID())
WHERE id = @id
UPDATE Customers
SET LastName = (SELECT TOP 1 LastName FROM Customers ORDER BY NEWID())
WHERE id = @id
FETCH NEXT FROM mycursor INTO @id;
END
CLOSE mycursor;
DEALLOCATE mycursor;
select * from Customers
答案 1 :(得分:3)
Redgate有它的工具:http://www.red-gate.com/products/SQL_Data_Generator/index.htm
没有使用它,但redgate工具非常好。
修改强>
它生成数据,而不是争夺,但仍然有用。
答案 2 :(得分:2)
我通过更改字段内的字母来加扰数据一次。所以,如果你有一个名字“迈克史密斯”并且你将所有的i改为o,那么m是l,s是e的,s是t,t是rr,你最终得到了
Moke Smoth
Loke Sloth
Loka Sloth
Loka Tloth
Loka Rrlorrh
这足以使名称不可读,而且你也不能回头确定它是什么(我改变了一些已经改变了字母的字母。)但是,它仍然具有可读性。
答案 3 :(得分:0)
不可能只将数据保留在表格中,并以某种方式仅以加密形式显示。
您的选择是通过以某种方式加扰数据来替换数据,生成具有相同通用形式的新数据,编写将其作为您使用的查询的一部分进行加扰的函数(CLR或T-SQL),或者加密数据,在这种情况下只有在用户也有适当的解密密钥时才能显示。
如果您决定替换数据,除了前面提到的Red Gate工具之外,您还可以考虑使用Visual Studio Team Database附带的数据生成器,或者也许使用Integration Services。如果您可以从更复杂的转型中受益,后者可能特别有用。
答案 4 :(得分:0)
dbForge有一个免费的数据生成工具:http://www.devart.com/dbforge/sql/data-generator/
答案 5 :(得分:0)
以下是一些具有相当不错的性能的简单方法,可以应用于表格:
use master;
declare @length as int = 50; --acts as maximum length for random length expressions
declare @rows as int = 10;
SELECT
CONVERT( VARCHAR(max), crypt_gen_random( @length )) as FixedLengthText
, CONVERT(NVARCHAR(max), crypt_gen_random( @length * 2 )) as FixedLengthUnicode
, ( select crypt_gen_random((@length/8*6))
where value."type" is not null --refer to outer query, to get different value for each row
FOR XML PATH('')) as FixedLengthBase64
, CONVERT( VARCHAR(max), crypt_gen_random( (ABS(CHECKSUM(NewId())) % @length )+1 )) as RandomLengthText
, CONVERT(NVARCHAR(max), crypt_gen_random( (ABS(CHECKSUM(NewId())) % (@length * 2))+1 )) as RandomLengthUnicode
, ( select crypt_gen_random( ( (ABS(CHECKSUM(NewId())) % @length )+1 )/8*6 )
where value."type" is not null --refer to outer query, to get different value for each row
FOR XML PATH('')) as RandomLengthBase64
FROM dbo.spt_values AS value
WHERE value."type" = 'P' --Limit "number" to integers between 0-2047
and value.number <= @rows
;
答案 6 :(得分:-1)
您可以创建需要更新的列的列表,然后简单地遍历所述列表并执行一些动态sql,它将以某种方式更新行。我做了一个相当基本的加扰函数,它只是对数据进行sha1(使用随机盐),因此它对于大多数用途来说应该足够安全。
if exists (select 1 where object_id('tempdb..#columnsToUpdate') is not null)
begin
drop table #columnsToUpdate
end
create table #columnsToUpdate(tableName varchar(max), columnName varchar(max), max_length int)
if exists (select 1 where object_id('fnGetSanitizedName') is not null)
begin
drop function fnGetSanitizedName
end
if exists (select 1 where object_id('random') is not null)
begin
drop view random
end
if exists (select 1 where object_id('randUniform') is not null)
begin
drop function randUniform
end
GO
create view random(value) as select rand();
go
create function dbo.randUniform() returns real
begin
declare @v real
set @v = (select value from random)
return @v
end
go
CREATE FUNCTION dbo.fnGetSanitizedName
(
@functionName nvarchar(max),
@length int
)
RETURNS varchar(max)
AS
BEGIN
return left(SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('SHA1', cast(cast(cast(dbo.randUniform() * 10000 as int) as varchar(8)) as varchar(40)) + @functionName)), 3, 32), @length)
END
GO
begin transaction
set nocount on
insert into #columnsToUpdate
select tables.name, columns.name,
case
when types.name = 'nvarchar' then columns.max_length / 2
else columns.max_length
end as max_length
from sys.tables tables
inner join sys.columns columns on tables.object_id=columns.object_id
inner join sys.types types on columns.system_type_id = types.system_type_id
where types.name in ('nvarchar', 'varchar')
declare @tableName varchar(max)
declare @columnName varchar(max)
declare @length int
declare @executingSql varchar(max)
declare tableUpdateCursor cursor
for select tableName, columnName, max_length from #columnsToUpdate
open tableUpdateCursor
fetch next from tableUpdateCursor into @tableName, @columnName, @length
while @@fetch_status = 0
begin
set @executingSql = 'update ' + @tableName + ' set ' + @columnName + ' = dbo.fnGetSanitizedName(' + @columnName + ',' + cast(@length as varchar(max)) + ')'
print @executingSql
exec(@executingSql)
fetch next from tableUpdateCursor into @tableName, @columnName, @length
end
close tableUpdateCursor
deallocate tableUpdateCursor
set nocount off
rollback -- Can remove the rollback when you are sure about what your are doing.
drop table #columnsToUpdate
drop function dbo.fnGetSanitizedName
drop view random
drop function randUniform