July 5, 2017 at 11:26 pm
Comments posted to this topic are about the item Dynamic SQL
____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
July 6, 2017 at 12:12 am
Good question thanks Stewart.
...
July 6, 2017 at 12:50 am
DECLARE @schema SYSNAME= 'TheSchema', --Include schema
---
SELECT
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_Name = @TableName
AND TABLE_SCHEMA = @schema -- there is no schema!
AND DATA_TYPE LIKE '%varchar';
---
SELECT
@SQL = N'SELECT @max-2 = MAX(DATALENGTH(' + QUOTENAME(@Col) + '))
FROM ' + QUOTENAME(@schema) + '.' + QUOTENAME(@TableName); --schema!
July 6, 2017 at 1:58 am
Darko Martinovic - Thursday, July 6, 2017 12:50 AMThank's the author for this question.
In the real world, this script would end with an error. The reason is trivial. There is no scheme in the script.
Eg. I look at a database model and in that model a table called 'Person'. There are 10 tables with that name, all in different schemas. In that case the script would produce an error.
But including the schema, everything works fine.DECLARE @schema SYSNAME= 'TheSchema', --Include schema
---SELECT
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_Name = @TableName
AND TABLE_SCHEMA = @schema -- there is no schema!
AND DATA_TYPE LIKE '%varchar';
---
SELECT
@SQL = N'SELECT @max-2 = MAX(DATALENGTH(' + QUOTENAME(@Col) + '))
FROM ' + QUOTENAME(@schema) + '.' + QUOTENAME(@TableName); --schema!
Fair point (a little beside the point, but fair none the less)
____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
July 6, 2017 at 2:02 am
Stewart "Arturius" Campbell - Thursday, July 6, 2017 1:58 AMDarko Martinovic - Thursday, July 6, 2017 12:50 AMThank's the author for this question.
In the real world, this script would end with an error. The reason is trivial. There is no scheme in the script.
Eg. I look at a database model and in that model a table called 'Person'. There are 10 tables with that name, all in different schemas. In that case the script would produce an error.
But including the schema, everything works fine.DECLARE @schema SYSNAME= 'TheSchema', --Include schema
---SELECT
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_Name = @TableName
AND TABLE_SCHEMA = @schema -- there is no schema!
AND DATA_TYPE LIKE '%varchar';
---
SELECT
@SQL = N'SELECT @max-2 = MAX(DATALENGTH(' + QUOTENAME(@Col) + '))
FROM ' + QUOTENAME(@schema) + '.' + QUOTENAME(@TableName); --schema!Fair point (a little beside the point, but fair none the less)
🙂
July 6, 2017 at 6:35 am
Darko Martinovic - Thursday, July 6, 2017 12:50 AMThank's the author for this question.
In the real world, this script would end with an error. The reason is trivial. There is no scheme in the script.
Eg. I look at a database model and in that model a table called 'Person'. There are 10 tables with that name, all in different schemas. In that case the script would produce an error.
But including the schema, everything works fine.DECLARE @schema SYSNAME= 'TheSchema', --Include schema
---SELECT
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_Name = @TableName
AND TABLE_SCHEMA = @schema -- there is no schema!
AND DATA_TYPE LIKE '%varchar';
---
SELECT
@SQL = N'SELECT @max-2 = MAX(DATALENGTH(' + QUOTENAME(@Col) + '))
FROM ' + QUOTENAME(@schema) + '.' + QUOTENAME(@TableName); --schema!
In your example, if the Person tables would have the same (N)VARCHAR-fields, and there is one table with the dbo-schema, the script would not generate any errors. The results however would not be what you expected.
July 6, 2017 at 6:39 am
Darko Martinovic - Thursday, July 6, 2017 12:50 AMThank's the author for this question.
In the real world, this script would end with an error. The reason is trivial. There is no scheme in the script.
Eg. I look at a database model and in that model a table called 'Person'. There are 10 tables with that name, all in different schemas. In that case the script would produce an error.
But including the schema, everything works fine.DECLARE @schema SYSNAME= 'TheSchema', --Include schema
---SELECT
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_Name = @TableName
AND TABLE_SCHEMA = @schema -- there is no schema!
AND DATA_TYPE LIKE '%varchar';
---
SELECT
@SQL = N'SELECT @max-2 = MAX(DATALENGTH(' + QUOTENAME(@Col) + '))
FROM ' + QUOTENAME(@schema) + '.' + QUOTENAME(@TableName); --schema!
And here I was thinking the problem was with the UPDATE statement as it didn't include the table name.
July 6, 2017 at 6:43 am
Mighty - Thursday, July 6, 2017 6:35 AMDarko Martinovic - Thursday, July 6, 2017 12:50 AMThank's the author for this question.
In the real world, this script would end with an error. The reason is trivial. There is no scheme in the script.
Eg. I look at a database model and in that model a table called 'Person'. There are 10 tables with that name, all in different schemas. In that case the script would produce an error.
But including the schema, everything works fine.DECLARE @schema SYSNAME= 'TheSchema', --Include schema
---SELECT
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_Name = @TableName
AND TABLE_SCHEMA = @schema -- there is no schema!
AND DATA_TYPE LIKE '%varchar';
---
SELECT
@SQL = N'SELECT @max-2 = MAX(DATALENGTH(' + QUOTENAME(@Col) + '))
FROM ' + QUOTENAME(@schema) + '.' + QUOTENAME(@TableName); --schema!In your example, if the Person tables would have the same (N)VARCHAR-fields, and there is one table with the dbo-schema, the script would not generate any errors. The results however would not be what you expected.
Yeah, right.
I did not want to write this because, as the author says, we turned a bit off the subject.
In any case, it is a good practice to use the scheme.
Thank you for your replay. All glory goes to the author. 🙂
July 6, 2017 at 6:53 am
lol I made a wild guess because "TLDR" wasn't a choice, easiest 2 points I ever got!
July 6, 2017 at 7:20 am
Luis Cazares - Thursday, July 6, 2017 6:39 AMAnd here I was thinking the problem was with the UPDATE statement as it didn't include the table name.
did i miss something?
UPDATE #Cols SET MaxLen = @max-2 WHERE ColumnName = @Col;
____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
July 6, 2017 at 7:27 am
Stewart "Arturius" Campbell - Thursday, July 6, 2017 7:20 AMLuis Cazares - Thursday, July 6, 2017 6:39 AMAnd here I was thinking the problem was with the UPDATE statement as it didn't include the table name.did i miss something?
UPDATE #Cols SET MaxLen = @max-2 WHERE ColumnName = @Col;
First of all, thank you for the question. I don't mean to be annoying.
The issue is similar to the schema problem. A column name can be the same for columns on different tables. Some people put a Description or Name column on every table. I would say that table (and schema) should be part of your table definition to fully identify the column.
July 6, 2017 at 11:25 am
July 6, 2017 at 11:50 am
Yes, if the target table existed in multiple schemas then you would have multiple column names returned in the cursor. I all the tables in the multiple schemas were identical, you would not have an issue, just that the query would run multiple times over the duplicate column names in the target table. If they were different, then you would have a problem. The target table also would be in the default schema for the user running the code.
I have written a lot of dynamic SQL and this was a fairly easy question to answer. What I was looking for was perhaps a missing OUTPUT, either in the definition of the parameter string or in the invokatiion of the sp_executesql itself.
I agree, one should always specify the schema of the table. Also, I wouldn't use INFORMATION_SCHEMA views, I would use the system views themselves (sys.tables, sys.columns, etc.).
July 7, 2017 at 1:32 am
Here a cursor free version:DECLARE @TableName SYSNAME = 'MyTable',@Schema SYSNAME = 'dbo', @sql NVARCHAR(MAX) = '', @max-2 INT
,@UnionALL NVARCHAR(11)= '';
SELECT @sql += @UnionAll + N'SELECT ''' + COLUMN_NAME + ''',MAX(DATALENGTH(' + QUOTENAME(COLUMN_NAME) + ')) FROM ' + QUOTENAME(@TableName)
,@UnionALL = ' UNION ALL '
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
AND TABLE_SCHEMA = @Schema
AND DATA_TYPE LIKE '%varchar';
EXEC sp_executesql @sql
Viewing 15 posts - 1 through 15 (of 20 total)
You must be logged in to reply to this topic. Login to reply