Skip to main content

Posts

Showing posts with the label T-SQL

SQL Get all Index create script from Database

To get all script from database as a create new index into another database you can use the following --Get all Index Script SELECT ' CREATE ' + CASE WHEN I.is_unique = 1 THEN ' UNIQUE ' ELSE '' END + I.type_desc COLLATE DATABASE_DEFAULT +' INDEX ' + I.name + ' ON ' + Schema_name(T.Schema_id)+'.'+T.name + ' ( ' + KeyColumns + ' ) ' + ISNULL(' INCLUDE ('+IncludedColumns+' ) ','') + ISNULL(' WHERE '+I.Filter_definition,'') + ' WITH ( ' + CASE WHEN I.is_padded = 1 THEN ' PAD_INDEX = ON ' ELSE ' PAD_INDEX = OFF ' END + ',' + 'FILLFACTOR = '+CONVERT(CHAR(5),CASE WHEN I.Fill_factor = 0 THEN 100 ELSE I.Fill_factor END) + ',' + -- default value 'SORT_IN_TEMPDB = OFF ' + ',' + CASE WHEN I.ignore_dup_key = 1 THEN ' IGNORE_DUP_KEY = ON ' ELSE ...

SQL Drop index script from Database

If yo want to get all drop index script from database you can use this. like '%idx_%'   = index prefix --Drop All Index declare @qry nvarchar(max); select @qry = (SELECT 'DROP INDEX ' + ix.name + ' ON ' + OBJECT_NAME(ID) + '; ' FROM sysindexes ix WHERE ix.Name IS NOT null and ix.Name like '%idx_%' for xml path('')); SELECT @qry This will return all drop index script on result. now copy the script for your use.

SQL get all table column which is null in database

create table #SuspectColumns ( TABLE_SCHEMA sysname, TABLE_NAME sysname, COLUMN_NAME sysname ) declare csrColumns cursor fast_forward for select TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where IS_NULLABLE = 'YES' declare @TABLE_SCHEMA sysname, @TABLE_NAME sysname, @COLUMN_NAME sysname, @sql nvarchar(max) open csrColumns while (1=1) begin fetch next from csrColumns into @TABLE_SCHEMA, @TABLE_NAME, @COLUMN_NAME if @@FETCH_STATUS<>0 break set @sql = N'if not exists(select 1 from ' + QUOTENAME(@TABLE_SCHEMA) + N'.' + QUOTENAME(@TABLE_NAME) + N' where ' + QUOTENAME(@COLUMN_NAME) + N'is not null) insert into #SuspectColumns values (''' + @TABLE_SCHEMA + N''',''' + @TABLE_NAME + N''',''' + @COLUMN_NAME + N''')' exec sp_executes...

Run all SQL files from a folder

Run all SQL files from  a folder you can follow the process that will save your time for executing SQL script Copy this code in a notepad file and change the Server, Database, Username And Password value as your self and save it as a .bat file. copy the file in your SQL script folder and run this bat file. REM REM development environment only!! REM pause for %%G in (*.sql) do sqlcmd /S "192.168.10.139\SQLEXPRESS" /d "VSSPORT_DEV" -U "atiour" -P "atiour" -i"%%G" pause REM REM All Script Run Successfully REM

sql replace coma seperated string

I have a string like this: 000014000608,000014000609,000014000610,000014000611 From this string i want to  remove 00001 with '' because this is a prefix of my every coma separated value. REPLACE((SUBSTRING(m_rx_nos,6,LEN(CAST(m_rx_nos AS VARCHAR(500)))-6)),(',0000'+ CONVERT(VARCHAR(100),f.pharminfoid_FK)),',') as m_rx_nos  Out Put: 4000608,4000609,4000610,400061

T-SQL update table from join table value

If you want to update your table from join table value you can try with this: UPDATE r SET r.l_auto_refill = 1 ,r.d_start_date = p.dDispDate ,r.n_frequency = p.nDispDaysSupply ,r.d_next_fill = DATEADD(DAY, p.ndispdayssupply, p.ddispdate) FROM rx r INNER join profile AS p ON r.[rxno_PK] = p.rxno_FK where r.rxno_PK = '000010578126'

T-SQL get one records from multiple same record based on max value for join

Let's say you have same type 6 record, into this record when you will picked data you will pick only one record which has highest value. SELECT * FROM rx r INNER join (SELECT distinct t1.* FROM profile t1 LEFT OUTER JOIN profile t2 ON (t1.rxno_FK = t2.rxno_FK AND t1.nrefillno < t2.nrefillno ) WHERE t2.dispid_PK IS NULL) AS p ON r.[rxno_PK] = p.rxno_FK