Skip to main content

Posts

Showing posts with the label microsoft sql

Scripting ALL ForeignKeys At Once!

I have a pretty extensive ETL in place involving a ton of tables. To improve performance and keep the transaction log clean, I recently decided to TRUNCATE a bunch of tables instead of DELETE. Unfortunately, Now, I was unable to truncate data as these tables are a part of foreign key based referential integrity in MS SQL. No love lost. I started scripting one FK at a time and saving them in two separate files, DROP_FK.sql and RECREATE_FK.sql. After doing about five of them at 11pm, I realized that this was going to be a painful task. A light bulb went off - why not use the system tables; they surely have all of this information stored somewhere! After a bit of research, I came up with the below query. There are many other queries (probably more robust than mine) out there on the interwebs! Please do your research. Without further ado, here is my query. Database: any Tables: sys.tables       sys.schemas       sys.foreign_keys     ...

Generate Jumbled/Randomized Words Using Custom SQL Function

To fill up my database with test data, I needed to generate dummy text, phone numbers, social security numbers etc. There are data generators out there, but i wanted to write my own, so here it is... ALTER FUNCTION dbo . fn_WORD_JUMBLER (       @Text VARCHAR ( MAX ) = '' ,       @DataType INT = 0 , -- 0 = random text, 1 = random number       @DataLenth INT = 10     ) RETURNS VARCHAR ( MAX ) AS BEGIN /* USAGE: SELECT dbo.fn_word_jumbler('ankeet Is Awesome!', DEFAULT, DEFAULT) SELECT dbo.fn_word_jumbler('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ', DEFAULT, DEFAULT) -- ALPHABETS SELECT dbo.fn_word_jumbler(DEFAULT, 0, DEFAULT) -- NUMBERS SELECT dbo.fn_word_jumbler(DEFAULT, 1, DEFAULT) ============================================================================= Created By: Ankeet Shah Purpose: Accepts an input and Returns the same text in reor...