SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Make script generic


Make script generic

Author
Message
khushbu
khushbu
SSC-Addicted
SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)

Group: General Forum Members
Points: 461 Visits: 752
Requirement-

I have 200+ columns whose data needs to be replaced with some random values i.e. mask the data.

The length of data type usually -varchar.

My query looks like - where column1 is being masked.

select column1,
substring (column1,1,2)+LEFT(REPLICATE(replace(newid(),'-',''),1+LEN(column1)/32),LEN(column1)-2)
from
schema1.table1
where len(column1) > 30

How do I make function (or something else )to make it more generic so that I can pass column, schema and table name more generically?
(since they all will change)

or is it worth making one?

Thanks for help

Khushbu
Steve Jones
Steve Jones
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: Administrators
Points: 64121 Visits: 19117
You can't use a variable in the FROM clause.

However if this is for something like masking data on restores to dev/test, and isn't run often, you can use dynamic-SQL

declare @cmd varchar(max)
declare @table1 varchar(200)
select @table1 = 'table1'

select @cmd = ' select column1, substring (column1,1,2) + '
+ 'LEFT(REPLICATE(replace(newid(),''-'',''''),1+LEN(column1)/32),LEN(column1)-2) '
+ 'from schema1.' + @table1
+ 'where len(column1) > 30'


Note that you can also make variables for the column and schema.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
caffeinated
caffeinated
SSC Journeyman
SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)

Group: General Forum Members
Points: 98 Visits: 241
I'm not sure how you can avoid creating a function/procedure or template.

I suggest using INFORMATION_SCHEMA views to generate the dynamic SQL.

Here's some sample SQL, but you'll probably need to CAST the "newid()" or a set of case statements to match your random value to match the appropriate datatype.



SELECT
schemaname = c.TABLE_SCHEMA
,tablename = c.TABLE_NAME
,columnname = c.COLUMN_NAME
,c.DATA_TYPE
,c.CHARACTER_MAXIMUM_LENGTH
,sqlquery = 'select [' + c.COLUMN_NAME + '] = newid() from [' + c.TABLE_SCHEMA + '].[' + c.TABLE_NAME + ']'
FROM INFORMATION_SCHEMA.COLUMNS c
--WHERE c.TABLE_SCHEMA = @schema
-- AND c.TABLE_NAME = @table

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search