Pass table name as parameter
I want to pass the table name as parameter and get all columns size of that table altered by column length + 50.
alter PROCEDURE dbo.testsp
@tablename varchar(100)
AS
BEGIN
DECLARE
@totalWeight TABLE (columnnames nvarchar(100));
INSERT INTO @totalWeight (columnnames)
SELECT name FROM sys.columns
WHERE object_id = object_id(@tablename);
END
GO
sql sql-server
add a comment |
I want to pass the table name as parameter and get all columns size of that table altered by column length + 50.
alter PROCEDURE dbo.testsp
@tablename varchar(100)
AS
BEGIN
DECLARE
@totalWeight TABLE (columnnames nvarchar(100));
INSERT INTO @totalWeight (columnnames)
SELECT name FROM sys.columns
WHERE object_id = object_id(@tablename);
END
GO
sql sql-server
you want to increase the column size by 50 chars ? Is this a one time effort or you are going to do this on regular basis ?
– Squirrel
Nov 21 '18 at 7:35
i have almost 300 tables
– adams
Nov 21 '18 at 7:37
i want to check max(length) of each column and alter column by max(length) + 50 chars
– adams
Nov 21 '18 at 7:37
2
it is not as simple as that. You need to check for data type. You only alter for string data type. And If you have any constraint on the table like primary key or foreign key, you can't just perform aalter column
like that. You will need to drop the constraint, alter the column and then re-create the constraints. Things gets even more complicated if you have foreign key constraints, you need to alter both end of the column before add back the constraint
– Squirrel
Nov 21 '18 at 7:52
gee squirrel, thats awfully true
– George Menoutis
Nov 21 '18 at 10:26
add a comment |
I want to pass the table name as parameter and get all columns size of that table altered by column length + 50.
alter PROCEDURE dbo.testsp
@tablename varchar(100)
AS
BEGIN
DECLARE
@totalWeight TABLE (columnnames nvarchar(100));
INSERT INTO @totalWeight (columnnames)
SELECT name FROM sys.columns
WHERE object_id = object_id(@tablename);
END
GO
sql sql-server
I want to pass the table name as parameter and get all columns size of that table altered by column length + 50.
alter PROCEDURE dbo.testsp
@tablename varchar(100)
AS
BEGIN
DECLARE
@totalWeight TABLE (columnnames nvarchar(100));
INSERT INTO @totalWeight (columnnames)
SELECT name FROM sys.columns
WHERE object_id = object_id(@tablename);
END
GO
sql sql-server
sql sql-server
edited Nov 21 '18 at 8:51
kit
1,1063616
1,1063616
asked Nov 21 '18 at 7:24
adams
456
456
you want to increase the column size by 50 chars ? Is this a one time effort or you are going to do this on regular basis ?
– Squirrel
Nov 21 '18 at 7:35
i have almost 300 tables
– adams
Nov 21 '18 at 7:37
i want to check max(length) of each column and alter column by max(length) + 50 chars
– adams
Nov 21 '18 at 7:37
2
it is not as simple as that. You need to check for data type. You only alter for string data type. And If you have any constraint on the table like primary key or foreign key, you can't just perform aalter column
like that. You will need to drop the constraint, alter the column and then re-create the constraints. Things gets even more complicated if you have foreign key constraints, you need to alter both end of the column before add back the constraint
– Squirrel
Nov 21 '18 at 7:52
gee squirrel, thats awfully true
– George Menoutis
Nov 21 '18 at 10:26
add a comment |
you want to increase the column size by 50 chars ? Is this a one time effort or you are going to do this on regular basis ?
– Squirrel
Nov 21 '18 at 7:35
i have almost 300 tables
– adams
Nov 21 '18 at 7:37
i want to check max(length) of each column and alter column by max(length) + 50 chars
– adams
Nov 21 '18 at 7:37
2
it is not as simple as that. You need to check for data type. You only alter for string data type. And If you have any constraint on the table like primary key or foreign key, you can't just perform aalter column
like that. You will need to drop the constraint, alter the column and then re-create the constraints. Things gets even more complicated if you have foreign key constraints, you need to alter both end of the column before add back the constraint
– Squirrel
Nov 21 '18 at 7:52
gee squirrel, thats awfully true
– George Menoutis
Nov 21 '18 at 10:26
you want to increase the column size by 50 chars ? Is this a one time effort or you are going to do this on regular basis ?
– Squirrel
Nov 21 '18 at 7:35
you want to increase the column size by 50 chars ? Is this a one time effort or you are going to do this on regular basis ?
– Squirrel
Nov 21 '18 at 7:35
i have almost 300 tables
– adams
Nov 21 '18 at 7:37
i have almost 300 tables
– adams
Nov 21 '18 at 7:37
i want to check max(length) of each column and alter column by max(length) + 50 chars
– adams
Nov 21 '18 at 7:37
i want to check max(length) of each column and alter column by max(length) + 50 chars
– adams
Nov 21 '18 at 7:37
2
2
it is not as simple as that. You need to check for data type. You only alter for string data type. And If you have any constraint on the table like primary key or foreign key, you can't just perform a
alter column
like that. You will need to drop the constraint, alter the column and then re-create the constraints. Things gets even more complicated if you have foreign key constraints, you need to alter both end of the column before add back the constraint– Squirrel
Nov 21 '18 at 7:52
it is not as simple as that. You need to check for data type. You only alter for string data type. And If you have any constraint on the table like primary key or foreign key, you can't just perform a
alter column
like that. You will need to drop the constraint, alter the column and then re-create the constraints. Things gets even more complicated if you have foreign key constraints, you need to alter both end of the column before add back the constraint– Squirrel
Nov 21 '18 at 7:52
gee squirrel, thats awfully true
– George Menoutis
Nov 21 '18 at 10:26
gee squirrel, thats awfully true
– George Menoutis
Nov 21 '18 at 10:26
add a comment |
2 Answers
2
active
oldest
votes
This query returns your alteration statements in the last column. You can then run them through dynamic sql.
SELECT
tbl.name 'Table Name',
c.name 'Column Name',
t.name,
c.max_length 'Max Length in Bytes',
c.precision ,
c.scale ,
c.is_nullable,
q1.*,
'alter table ['
+tbl.name
+'] alter column ['
+c.name
+'] '
+t.name
+'('+
convert(nvarchar(1000), CASE WHEN t.name IN ('nchar','nvarchar') THEN c.max_length/2 ELSE c.max_length END +50)
+') '
+ case c.is_nullable when 0 then ' not null' else ' null' end
as 'Alteration statement'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id = t.user_type_id
inner join sys.tables tbl on tbl.object_id=c.object_id
CROSS APPLY
(
select
t.name +
CASE WHEN t.name IN ('char', 'varchar','nchar','nvarchar') THEN '('+
CASE WHEN c.max_length=-1 THEN 'MAX'
ELSE CONVERT(VARCHAR(4),
CASE WHEN t.name IN ('nchar','nvarchar')
THEN c.max_length/2 ELSE c.max_length END )
END +')'
WHEN t.name IN ('decimal','numeric')
THEN '('+ CONVERT(VARCHAR(4),c.precision)+','
+ CONVERT(VARCHAR(4),c.Scale)+')'
ELSE '' END
+ case c.is_nullable when 0 then ' not null' else ' null' end
as [DDL name before]
)q1
where t.name IN ('char', 'varchar','nchar','nvarchar') and c.max_length>-1
i want to find max(length) of each nvarchar column by passing table name and alter that column with +50 chars
– adams
Nov 21 '18 at 8:22
the statement is right there in the last column. Do you need help for the dynamic sql and the cursor part, too?
– George Menoutis
Nov 21 '18 at 10:01
yes ,can you help out
– adams
Nov 21 '18 at 10:14
suppose a column length is nvarchar(200).. and data in that column is 20 char. i want a script which give alter statement like nvarchar(70).... maximum data length of that column + 50 char
– adams
Nov 21 '18 at 10:29
add a comment |
Run the following example of query, it creates a bunch of ALTER statements. Copy-paste the result into a new query window and run it again. Add other data types from sys.types
if need.
SELECT 'ALTER TABLE [' + s.name + '].[' + t.name + '] ALTER COLUMN [' +
c.name + '] ' + ty.name + '(' + CAST(c.max_length + 50 AS nvarchar(10)) + ') ' +
CASE WHEN c.is_nullable = 1 THEN 'NULL' ELSE 'NOT NULL' END + ';'
FROM sys.tables t
INNER JOIN sys.columns c ON t.object_id = c.object_id
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
INNER JOIN sys.types ty ON c.system_type_id = ty.system_type_id
WHERE t.type = 'U' AND
c.system_type_id IN (167 /*varchar*/, 231 /*nvarchar*/)
i want something like this...SELECT 'ALTER TABLE [' + table_schema + '].[' + table_name + '] ALTER COLUMN [' + column_name + '] ' + '(' + CAST((SELECT MAX(DATALENGTH('+ COLUMN_NAME+ ' + 50 ) + ')' FROM INFORMATION_SCHEMA.COLUMNS
– adams
Nov 21 '18 at 12:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407083%2fpass-table-name-as-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This query returns your alteration statements in the last column. You can then run them through dynamic sql.
SELECT
tbl.name 'Table Name',
c.name 'Column Name',
t.name,
c.max_length 'Max Length in Bytes',
c.precision ,
c.scale ,
c.is_nullable,
q1.*,
'alter table ['
+tbl.name
+'] alter column ['
+c.name
+'] '
+t.name
+'('+
convert(nvarchar(1000), CASE WHEN t.name IN ('nchar','nvarchar') THEN c.max_length/2 ELSE c.max_length END +50)
+') '
+ case c.is_nullable when 0 then ' not null' else ' null' end
as 'Alteration statement'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id = t.user_type_id
inner join sys.tables tbl on tbl.object_id=c.object_id
CROSS APPLY
(
select
t.name +
CASE WHEN t.name IN ('char', 'varchar','nchar','nvarchar') THEN '('+
CASE WHEN c.max_length=-1 THEN 'MAX'
ELSE CONVERT(VARCHAR(4),
CASE WHEN t.name IN ('nchar','nvarchar')
THEN c.max_length/2 ELSE c.max_length END )
END +')'
WHEN t.name IN ('decimal','numeric')
THEN '('+ CONVERT(VARCHAR(4),c.precision)+','
+ CONVERT(VARCHAR(4),c.Scale)+')'
ELSE '' END
+ case c.is_nullable when 0 then ' not null' else ' null' end
as [DDL name before]
)q1
where t.name IN ('char', 'varchar','nchar','nvarchar') and c.max_length>-1
i want to find max(length) of each nvarchar column by passing table name and alter that column with +50 chars
– adams
Nov 21 '18 at 8:22
the statement is right there in the last column. Do you need help for the dynamic sql and the cursor part, too?
– George Menoutis
Nov 21 '18 at 10:01
yes ,can you help out
– adams
Nov 21 '18 at 10:14
suppose a column length is nvarchar(200).. and data in that column is 20 char. i want a script which give alter statement like nvarchar(70).... maximum data length of that column + 50 char
– adams
Nov 21 '18 at 10:29
add a comment |
This query returns your alteration statements in the last column. You can then run them through dynamic sql.
SELECT
tbl.name 'Table Name',
c.name 'Column Name',
t.name,
c.max_length 'Max Length in Bytes',
c.precision ,
c.scale ,
c.is_nullable,
q1.*,
'alter table ['
+tbl.name
+'] alter column ['
+c.name
+'] '
+t.name
+'('+
convert(nvarchar(1000), CASE WHEN t.name IN ('nchar','nvarchar') THEN c.max_length/2 ELSE c.max_length END +50)
+') '
+ case c.is_nullable when 0 then ' not null' else ' null' end
as 'Alteration statement'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id = t.user_type_id
inner join sys.tables tbl on tbl.object_id=c.object_id
CROSS APPLY
(
select
t.name +
CASE WHEN t.name IN ('char', 'varchar','nchar','nvarchar') THEN '('+
CASE WHEN c.max_length=-1 THEN 'MAX'
ELSE CONVERT(VARCHAR(4),
CASE WHEN t.name IN ('nchar','nvarchar')
THEN c.max_length/2 ELSE c.max_length END )
END +')'
WHEN t.name IN ('decimal','numeric')
THEN '('+ CONVERT(VARCHAR(4),c.precision)+','
+ CONVERT(VARCHAR(4),c.Scale)+')'
ELSE '' END
+ case c.is_nullable when 0 then ' not null' else ' null' end
as [DDL name before]
)q1
where t.name IN ('char', 'varchar','nchar','nvarchar') and c.max_length>-1
i want to find max(length) of each nvarchar column by passing table name and alter that column with +50 chars
– adams
Nov 21 '18 at 8:22
the statement is right there in the last column. Do you need help for the dynamic sql and the cursor part, too?
– George Menoutis
Nov 21 '18 at 10:01
yes ,can you help out
– adams
Nov 21 '18 at 10:14
suppose a column length is nvarchar(200).. and data in that column is 20 char. i want a script which give alter statement like nvarchar(70).... maximum data length of that column + 50 char
– adams
Nov 21 '18 at 10:29
add a comment |
This query returns your alteration statements in the last column. You can then run them through dynamic sql.
SELECT
tbl.name 'Table Name',
c.name 'Column Name',
t.name,
c.max_length 'Max Length in Bytes',
c.precision ,
c.scale ,
c.is_nullable,
q1.*,
'alter table ['
+tbl.name
+'] alter column ['
+c.name
+'] '
+t.name
+'('+
convert(nvarchar(1000), CASE WHEN t.name IN ('nchar','nvarchar') THEN c.max_length/2 ELSE c.max_length END +50)
+') '
+ case c.is_nullable when 0 then ' not null' else ' null' end
as 'Alteration statement'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id = t.user_type_id
inner join sys.tables tbl on tbl.object_id=c.object_id
CROSS APPLY
(
select
t.name +
CASE WHEN t.name IN ('char', 'varchar','nchar','nvarchar') THEN '('+
CASE WHEN c.max_length=-1 THEN 'MAX'
ELSE CONVERT(VARCHAR(4),
CASE WHEN t.name IN ('nchar','nvarchar')
THEN c.max_length/2 ELSE c.max_length END )
END +')'
WHEN t.name IN ('decimal','numeric')
THEN '('+ CONVERT(VARCHAR(4),c.precision)+','
+ CONVERT(VARCHAR(4),c.Scale)+')'
ELSE '' END
+ case c.is_nullable when 0 then ' not null' else ' null' end
as [DDL name before]
)q1
where t.name IN ('char', 'varchar','nchar','nvarchar') and c.max_length>-1
This query returns your alteration statements in the last column. You can then run them through dynamic sql.
SELECT
tbl.name 'Table Name',
c.name 'Column Name',
t.name,
c.max_length 'Max Length in Bytes',
c.precision ,
c.scale ,
c.is_nullable,
q1.*,
'alter table ['
+tbl.name
+'] alter column ['
+c.name
+'] '
+t.name
+'('+
convert(nvarchar(1000), CASE WHEN t.name IN ('nchar','nvarchar') THEN c.max_length/2 ELSE c.max_length END +50)
+') '
+ case c.is_nullable when 0 then ' not null' else ' null' end
as 'Alteration statement'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id = t.user_type_id
inner join sys.tables tbl on tbl.object_id=c.object_id
CROSS APPLY
(
select
t.name +
CASE WHEN t.name IN ('char', 'varchar','nchar','nvarchar') THEN '('+
CASE WHEN c.max_length=-1 THEN 'MAX'
ELSE CONVERT(VARCHAR(4),
CASE WHEN t.name IN ('nchar','nvarchar')
THEN c.max_length/2 ELSE c.max_length END )
END +')'
WHEN t.name IN ('decimal','numeric')
THEN '('+ CONVERT(VARCHAR(4),c.precision)+','
+ CONVERT(VARCHAR(4),c.Scale)+')'
ELSE '' END
+ case c.is_nullable when 0 then ' not null' else ' null' end
as [DDL name before]
)q1
where t.name IN ('char', 'varchar','nchar','nvarchar') and c.max_length>-1
answered Nov 21 '18 at 7:55
George Menoutis
2,519319
2,519319
i want to find max(length) of each nvarchar column by passing table name and alter that column with +50 chars
– adams
Nov 21 '18 at 8:22
the statement is right there in the last column. Do you need help for the dynamic sql and the cursor part, too?
– George Menoutis
Nov 21 '18 at 10:01
yes ,can you help out
– adams
Nov 21 '18 at 10:14
suppose a column length is nvarchar(200).. and data in that column is 20 char. i want a script which give alter statement like nvarchar(70).... maximum data length of that column + 50 char
– adams
Nov 21 '18 at 10:29
add a comment |
i want to find max(length) of each nvarchar column by passing table name and alter that column with +50 chars
– adams
Nov 21 '18 at 8:22
the statement is right there in the last column. Do you need help for the dynamic sql and the cursor part, too?
– George Menoutis
Nov 21 '18 at 10:01
yes ,can you help out
– adams
Nov 21 '18 at 10:14
suppose a column length is nvarchar(200).. and data in that column is 20 char. i want a script which give alter statement like nvarchar(70).... maximum data length of that column + 50 char
– adams
Nov 21 '18 at 10:29
i want to find max(length) of each nvarchar column by passing table name and alter that column with +50 chars
– adams
Nov 21 '18 at 8:22
i want to find max(length) of each nvarchar column by passing table name and alter that column with +50 chars
– adams
Nov 21 '18 at 8:22
the statement is right there in the last column. Do you need help for the dynamic sql and the cursor part, too?
– George Menoutis
Nov 21 '18 at 10:01
the statement is right there in the last column. Do you need help for the dynamic sql and the cursor part, too?
– George Menoutis
Nov 21 '18 at 10:01
yes ,can you help out
– adams
Nov 21 '18 at 10:14
yes ,can you help out
– adams
Nov 21 '18 at 10:14
suppose a column length is nvarchar(200).. and data in that column is 20 char. i want a script which give alter statement like nvarchar(70).... maximum data length of that column + 50 char
– adams
Nov 21 '18 at 10:29
suppose a column length is nvarchar(200).. and data in that column is 20 char. i want a script which give alter statement like nvarchar(70).... maximum data length of that column + 50 char
– adams
Nov 21 '18 at 10:29
add a comment |
Run the following example of query, it creates a bunch of ALTER statements. Copy-paste the result into a new query window and run it again. Add other data types from sys.types
if need.
SELECT 'ALTER TABLE [' + s.name + '].[' + t.name + '] ALTER COLUMN [' +
c.name + '] ' + ty.name + '(' + CAST(c.max_length + 50 AS nvarchar(10)) + ') ' +
CASE WHEN c.is_nullable = 1 THEN 'NULL' ELSE 'NOT NULL' END + ';'
FROM sys.tables t
INNER JOIN sys.columns c ON t.object_id = c.object_id
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
INNER JOIN sys.types ty ON c.system_type_id = ty.system_type_id
WHERE t.type = 'U' AND
c.system_type_id IN (167 /*varchar*/, 231 /*nvarchar*/)
i want something like this...SELECT 'ALTER TABLE [' + table_schema + '].[' + table_name + '] ALTER COLUMN [' + column_name + '] ' + '(' + CAST((SELECT MAX(DATALENGTH('+ COLUMN_NAME+ ' + 50 ) + ')' FROM INFORMATION_SCHEMA.COLUMNS
– adams
Nov 21 '18 at 12:34
add a comment |
Run the following example of query, it creates a bunch of ALTER statements. Copy-paste the result into a new query window and run it again. Add other data types from sys.types
if need.
SELECT 'ALTER TABLE [' + s.name + '].[' + t.name + '] ALTER COLUMN [' +
c.name + '] ' + ty.name + '(' + CAST(c.max_length + 50 AS nvarchar(10)) + ') ' +
CASE WHEN c.is_nullable = 1 THEN 'NULL' ELSE 'NOT NULL' END + ';'
FROM sys.tables t
INNER JOIN sys.columns c ON t.object_id = c.object_id
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
INNER JOIN sys.types ty ON c.system_type_id = ty.system_type_id
WHERE t.type = 'U' AND
c.system_type_id IN (167 /*varchar*/, 231 /*nvarchar*/)
i want something like this...SELECT 'ALTER TABLE [' + table_schema + '].[' + table_name + '] ALTER COLUMN [' + column_name + '] ' + '(' + CAST((SELECT MAX(DATALENGTH('+ COLUMN_NAME+ ' + 50 ) + ')' FROM INFORMATION_SCHEMA.COLUMNS
– adams
Nov 21 '18 at 12:34
add a comment |
Run the following example of query, it creates a bunch of ALTER statements. Copy-paste the result into a new query window and run it again. Add other data types from sys.types
if need.
SELECT 'ALTER TABLE [' + s.name + '].[' + t.name + '] ALTER COLUMN [' +
c.name + '] ' + ty.name + '(' + CAST(c.max_length + 50 AS nvarchar(10)) + ') ' +
CASE WHEN c.is_nullable = 1 THEN 'NULL' ELSE 'NOT NULL' END + ';'
FROM sys.tables t
INNER JOIN sys.columns c ON t.object_id = c.object_id
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
INNER JOIN sys.types ty ON c.system_type_id = ty.system_type_id
WHERE t.type = 'U' AND
c.system_type_id IN (167 /*varchar*/, 231 /*nvarchar*/)
Run the following example of query, it creates a bunch of ALTER statements. Copy-paste the result into a new query window and run it again. Add other data types from sys.types
if need.
SELECT 'ALTER TABLE [' + s.name + '].[' + t.name + '] ALTER COLUMN [' +
c.name + '] ' + ty.name + '(' + CAST(c.max_length + 50 AS nvarchar(10)) + ') ' +
CASE WHEN c.is_nullable = 1 THEN 'NULL' ELSE 'NOT NULL' END + ';'
FROM sys.tables t
INNER JOIN sys.columns c ON t.object_id = c.object_id
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
INNER JOIN sys.types ty ON c.system_type_id = ty.system_type_id
WHERE t.type = 'U' AND
c.system_type_id IN (167 /*varchar*/, 231 /*nvarchar*/)
answered Nov 21 '18 at 9:55
serge
59537
59537
i want something like this...SELECT 'ALTER TABLE [' + table_schema + '].[' + table_name + '] ALTER COLUMN [' + column_name + '] ' + '(' + CAST((SELECT MAX(DATALENGTH('+ COLUMN_NAME+ ' + 50 ) + ')' FROM INFORMATION_SCHEMA.COLUMNS
– adams
Nov 21 '18 at 12:34
add a comment |
i want something like this...SELECT 'ALTER TABLE [' + table_schema + '].[' + table_name + '] ALTER COLUMN [' + column_name + '] ' + '(' + CAST((SELECT MAX(DATALENGTH('+ COLUMN_NAME+ ' + 50 ) + ')' FROM INFORMATION_SCHEMA.COLUMNS
– adams
Nov 21 '18 at 12:34
i want something like this...SELECT 'ALTER TABLE [' + table_schema + '].[' + table_name + '] ALTER COLUMN [' + column_name + '] ' + '(' + CAST((SELECT MAX(DATALENGTH('+ COLUMN_NAME+ ' + 50 ) + ')' FROM INFORMATION_SCHEMA.COLUMNS
– adams
Nov 21 '18 at 12:34
i want something like this...SELECT 'ALTER TABLE [' + table_schema + '].[' + table_name + '] ALTER COLUMN [' + column_name + '] ' + '(' + CAST((SELECT MAX(DATALENGTH('+ COLUMN_NAME+ ' + 50 ) + ')' FROM INFORMATION_SCHEMA.COLUMNS
– adams
Nov 21 '18 at 12:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407083%2fpass-table-name-as-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you want to increase the column size by 50 chars ? Is this a one time effort or you are going to do this on regular basis ?
– Squirrel
Nov 21 '18 at 7:35
i have almost 300 tables
– adams
Nov 21 '18 at 7:37
i want to check max(length) of each column and alter column by max(length) + 50 chars
– adams
Nov 21 '18 at 7:37
2
it is not as simple as that. You need to check for data type. You only alter for string data type. And If you have any constraint on the table like primary key or foreign key, you can't just perform a
alter column
like that. You will need to drop the constraint, alter the column and then re-create the constraints. Things gets even more complicated if you have foreign key constraints, you need to alter both end of the column before add back the constraint– Squirrel
Nov 21 '18 at 7:52
gee squirrel, thats awfully true
– George Menoutis
Nov 21 '18 at 10:26