加任意 bytes 數量的亂數值

use tempdb
go
CREATE VIEW rndView
AS
SELECT RAND() rndResult
GO

create function RandBytes
(
    @ByteLength int
)
returns varbinary(max)
as
begin
    declare @ret varbinary(max)=0x,@i int=0,@rndValue tinyint
    while @i<@ByteLength
    begin
        select @rndValue = convert(tinyint,rndResult*256)
        from rndView
        set @ret+=convert(binary(1),@rndValue)
        set @i+=1
    end
    return @ret
end
GO

create table #t(c1 nvarchar(10))
insert #t values(N’中文’),(N’測試一下’)

select dbo.RandBytes(10)+convert(varbinary(max),c1),
substring(dbo.RandBytes(10)+convert(varbinary(max),c1),11,datalength(convert(varbinary(max),c1))),
convert(nvarchar(max),substring(dbo.RandBytes(10)+convert(varbinary(max),c1),11,datalength(convert(varbinary(max),c1))))
from #t

drop table #t

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

%d 位部落客按了讚: