2013-12-24 33 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 
相关问题