August 4, 2016 at 10:56 am
The following works fine, I run this , copy the output paste it in another window, hit F5 and I am done!
Wonder if someone can modify the code so that it can run the DDL statement as well ( Without me having to copy and paste )
Declare @SCHEMA VARCHAR(100);
Declare @TBL_PREFIX VARCHAR(100);
Select @SCHEMA = 'CLAIM'
Select @TBL_PREFIX = 'IMP'
Select
'ALTER TABLE ' + TABLE_SCHEMA + '.' + table_name + ' ALTER COLUMN ' + COLUMN_NAME + ' ' + DATA_TYPE + '(' + CAST( CHARACTER_MAXIMUM_LENGTH as VARCHAR ) + ') NOT NULL; '
FROM INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA = @SCHEMA and table_name like @TBL_PREFIX + '%'
and
COLUMN_NAME in ('SourceID', 'ClaimNumber', 'ClaimLineNumber','DataSourceType','FirstServiceDate','Denied','MemberID','ProductID','ProviderID','AsOfDate' )
AND
DATA_TYPE in ( 'VARCHAR' )
ORDER BY 1
August 4, 2016 at 11:14 am
Assign it to a variable and then use sp_executesql
Here's another link: https://technet.microsoft.com/en-us/library/ms175170(v=sql.105).aspx
August 4, 2016 at 11:19 am
Not sure since it produces more than one row as output.. Some help would be appreciated!
August 4, 2016 at 11:30 am
You could do it with the following method: http://www.sqlservercentral.com/articles/comma+separated+list/71700/
DECLARE @sql NVARCHAR(MAX);
SELECT @sql = ( Select 'ALTER TABLE ' + TABLE_SCHEMA + '.' + table_name
+ ' ALTER COLUMN ' + COLUMN_NAME + ' ' + DATA_TYPE
+ '(' + CAST( CHARACTER_MAXIMUM_LENGTH as VARCHAR ) + ') NOT NULL; '
FROM INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = @SCHEMA
and table_name like @TBL_PREFIX + '%'
and COLUMN_NAME in ('SourceID', 'ClaimNumber', 'ClaimLineNumber','DataSourceType','FirstServiceDate','Denied','MemberID','ProductID','ProviderID','AsOfDate' )
AND DATA_TYPE in ( 'VARCHAR' )
ORDER BY 1
FOR XML PATH(''), TYPE).value('./text()[1]', 'varchar(max)')
PRINT @sql;
Or you could go "old school"
DECLARE @sql NVARCHAR(MAX) = '';
SELECT @sql = @sql + 'ALTER TABLE ' + TABLE_SCHEMA + '.' + table_name
+ ' ALTER COLUMN ' + COLUMN_NAME + ' ' + DATA_TYPE
+ '(' + CAST( CHARACTER_MAXIMUM_LENGTH as VARCHAR ) + ') NOT NULL; '
FROM INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = @SCHEMA
and table_name like @TBL_PREFIX + '%'
and COLUMN_NAME in ('SourceID', 'ClaimNumber', 'ClaimLineNumber','DataSourceType','FirstServiceDate','Denied','MemberID','ProductID','ProviderID','AsOfDate' )
AND DATA_TYPE in ( 'VARCHAR' );
PRINT @sql;
I prefer the first method since it's easier to modify and control.
August 4, 2016 at 1:29 pm
Done! Works!
Thanks
Declare @SCHEMA VARCHAR(100);
Declare @TBL_PREFIX VARCHAR(100);
Select @SCHEMA = 'CLAIM'
Select @TBL_PREFIX = 'IMP'
DECLARE @sql NVARCHAR(MAX);
SELECT @sql = ( Select 'ALTER TABLE ' + TABLE_SCHEMA + '.' + table_name
+ ' ALTER COLUMN ' + COLUMN_NAME + ' ' + DATA_TYPE
+ '(' + CAST( CHARACTER_MAXIMUM_LENGTH as VARCHAR ) + ') NULL; '
FROM INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = @SCHEMA
and table_name like @TBL_PREFIX + '%'
and COLUMN_NAME in ('ProviderID')
AND DATA_TYPE in ( 'VARCHAR' )
ORDER BY 1
FOR XML PATH(''), TYPE).value('./text()[1]', 'varchar(max)');
--SELECT @sql;
EXECUTE sp_executesql @sql;
August 4, 2016 at 1:34 pm
mw112009 (8/4/2016)
Done! Works!Thanks
Do you understand how it works? The article should explain it clearly, but you can post any questions after reading it.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply