Way to print a record with one row for each field containing key, field name (column name), and field value (column value) in SQL
/* This code clears and reloads the PCS_PRODUCT_ATTRIBUTE table with the prod as first column
followed by the field name and field value for each name in a given table.
it outputs this information in item_key order followed by attributes in the order they
are defined in the table based on the ordinal position of the field.
For example, if icsp were used, the fields would be cono, lookupnm, prodcat, transdt,
transtm, etc.
The table will be loaded with all items and cono first, then all items and lookupnm, then
all items and prodcat, etc.
It is coded not to put out a line if a field is null, zero, or blank'
Current there is a TOP 20 in the select query for going through icsp without hitting
every item.
*/
followed by the field name and field value for each name in a given table.
it outputs this information in item_key order followed by attributes in the order they
are defined in the table based on the ordinal position of the field.
For example, if icsp were used, the fields would be cono, lookupnm, prodcat, transdt,
transtm, etc.
The table will be loaded with all items and cono first, then all items and lookupnm, then
all items and prodcat, etc.
It is coded not to put out a line if a field is null, zero, or blank'
Current there is a TOP 20 in the select query for going through icsp without hitting
every item.
*/
use sximage Declare @TN as varchar(200), @CN as varchar(200), @DT as varchar(30), @OP as int, @item_key varchar(30), @SQL as nvarchar(1000), @SN as varchar(200), @Exact_Match bit -- this field name will be bypassed and printed as the actual ITEM_KEY in the table set @item_key = 'prod' delete pcs.dbo.PCS_PRODUCT_ATTRIBUTE Declare myCursor Cursor For Select T.Table_Name, C.Column_Name, C.Data_Type, C.Ordinal_Position, T.Table_Schema From INFORMATION_SCHEMA.TABLES T Inner Join INFORMATION_SCHEMA.COLUMNS C On T.Table_Schema = C.Table_Schema And T.Table_Name = C.Table_Name -- Change this table name to a specific table or like 'ATT_%' if all tables -- for attributes start with the same characters. Where T.Table_Name = 'icsp_live' and C.Column_Name<>@item_key order by 1, 4 -- and C.Data_Type not in ('int','decimal') Open myCursor Fetch Next From myCursor Into @TN, @CN, @DT, @OP, @SN While @@Fetch_Status <> -1 Begin Set @SQL = N'Insert into pcs.dbo.PCS_PRODUCT_ATTRIBUTE Select top 20 [' + @item_key + '] as ITEM_KEY, ''' + @CN + ''' as ATTRIBUTE_KEY, [' + @CN + '] as ATTRIBUTE_VALUE, NULL as CATEGORY_KEY, NULL as ATTRIBUTE_LOOKUP_KEY, NULL as FLAG_PRIMARY, NULL as DIVISION_KEY From [' + @SN + '].[' + @TN + '] Where [' + @CN + '] <> NULL AND ( ''' + @DT + ''' not in (''' + 'int' + ''',''' + 'decimal' + ''') AND LTRIM(RTRIM([' + @CN + '])) <> ''' + ''' OR ''' + @DT + ''' in (''' + 'int' + ''',''' + 'decimal' + ''') AND [' + @CN + '] <> 0 ) /* ''' + @item_key + '''*/' --Print @SQL Exec sp_executesql @SQL Fetch Next From myCursor Into @TN, @CN, @DT, @OP, @SN End Close myCursor Deallocate myCursor