2013-12-24 84 views
0

我正在通過將字符字母向下移動一個由鍵給出的插槽數來加密表名和列名。 例如,當key = 3時,「A」變成「D」,「z」變成「c」,並且「1」變成「4」。如何根據sql server中的舊名稱編輯列名和表名?

這裏是我的代碼加載數據:

USE testdata 
GO 
CREATE testtable 
    (TREATMENT CHAR(20), 
    PRICE INT) 
GO 

BULK INSERT testtable 
FROM 'c:\testdata.txt' 
WITH 
(
    FIELDTERMINATOR = ',', 
    ROWTERMINATOR = '\n' 
) 
GO 

SELECT * 
FROM testtable 
GO 

我怎麼能這樣做呢?我需要動態SQL嗎?

謝謝!

+0

你改變了列名,表名? –

+0

是的,但必須根據舊錶名和列名來做到這一點。 – Autumn

+0

我認爲混淆你的數據庫模式是一個非常糟糕的主意。你打開了自己的整個世界...... –

回答

0

我已經寫了一個程序來改變字符串,但不知道如何使用它的列名

create procedure encrypt 
    @ptext as varchar(500) 
as 
begin 
set nocount on 
declare @key as tinyint = 3 
declare @etext as varchar(500) = ‘ ’ 
declare @pc as varchar(1) 
declare @i as smallint = 1 
declare @n as smallint 

set @n = len(@ptext) 

while @i <= @n 
begin 
set @pc = substring (@ptext, @i, 1) 

if ascii(@pc) between 48 and 57 
begin 
    if ascii(@pc) + @key < 58 
     set @pc = char((ascii(@pc) + @key)) 
    else 
     set @pc = char((ascii(@pc) + @key)-10) 
end 

else if ascii(@pc) between 65 and 90 
begin 
    if ascii(@pc) + @key < 91 
     set @pc = char((ascii(@pc) + @key)) 
    else 
     set @pc = char((ascii(@pc) + @key)-26) 
end 

if ascii(@pc) between 97 and 122 
begin 
    if ascii(@pc) + @key < 123 
     set @pc = char((ascii(@pc) + @key)) 
    else 
     set @pc = char((ascii(@pc) + @key)-26) 
end 

set @etext = @etext + @pc 
set @i = @i + 1 
end 
select @etext 
end 
相關問題