T-SQL To Generate Select and Insert into scripts only for columns that have values
Budget: $10 – $30 USD
I have a script that will generate the Select statement for only columns that are not nullable. But, what I need is a script that will generate the Select and Insert into statements for only columns that have values in the table. That is because we always have dozens of columns that are not used. Database create script attached. Sql Server 2016
DECLARE @table nvarchar(261) = N'[dbo].[table1]';
;WITH x AS
(
SELECT name, system_type_name,
column_ordinal = ROW_NUMBER() OVER (ORDER BY column_ordinal)
FROM sys.dm_exec_describe_first_result_set(N'SELECT * FROM ' + @table, N'', 0)
WHERE is_nullable = 0
AND is_identity_column = 0
AND is_computed_column = 0
)
SELECT N'INSERT ' + @table + N'(', N'--', column_ordinal = 0
UNION ALL
SELECT CHAR(13) + CHAR(10) + CASE column_ordinal
WHEN 1 THEN N'' ELSE N',' END + QUOTENAME(name),
N'--', column_ordinal
FROM x
UNION ALL
SELECT N'
) VALUES (', N'--', 1000
UNION ALL
SELECT CHAR(13) + CHAR(10) + CASE column_ordinal
WHEN 1 THEN N'' ELSE N',' END
+ N'<' + name + N', ' +system_type_name + N',>',
N'--', 1000 + column_ordinal
FROM x
UNION ALL
SELECT N'
);', N'--', 10000
ORDER BY column_ordinal;
*/
/*
INSERT [dbo].[table1](
[IDNUM]
,[c1]
,[c2]
,[c3]
,[c7]
) VALUES (
<IDNUM, int,>
,<c1, varchar(9),>
,<c2, int,>
,<c3, int,>
,<c7, numeric(8,2),>
);
*/
Please reply with:
The time of day that you are available. I am available 7am to 10pm UTC-06:00 Central Time US & Canada. Will you require contact by voice and do you speak English? When you can complete the project.
Confidentiality.
a) No Use. Recipient agrees not to use the Confidential Information in any way, or to manufacture or test any product embodying Confidential Information, except for the purpose set forth above.
b) No Disclosure. Recipient agrees to use its best efforts to prevent and protect the Confidential Information, or any part thereof, from disclosure to any person other than Recipient's employees having a need for disclosure in connection with Recipient's authorized use of the Confidential Information.
c) Protection of Secrecy. Recipient agrees to take all steps reasonably necessary to protect the secrecy of the Confidential Information, and to prevent the Confidential Information from falling into the public domain or into the possession of unauthorized persons.
d) Scope. The scope of Confidentiality is deemed to be in all contracts present past and future with the Parties to this request
DECLARE @table nvarchar(261) = N'[dbo].[table1]';
;WITH x AS
(
SELECT name, system_type_name,
column_ordinal = ROW_NUMBER() OVER (ORDER BY column_ordinal)
FROM sys.dm_exec_describe_first_result_set(N'SELECT * FROM ' + @table, N'', 0)
WHERE is_nullable = 0
AND is_identity_column = 0
AND is_computed_column = 0
)
SELECT N'INSERT ' + @table + N'(', N'--', column_ordinal = 0
UNION ALL
SELECT CHAR(13) + CHAR(10) + CASE column_ordinal
WHEN 1 THEN N'' ELSE N',' END + QUOTENAME(name),
N'--', column_ordinal
FROM x
UNION ALL
SELECT N'
) VALUES (', N'--', 1000
UNION ALL
SELECT CHAR(13) + CHAR(10) + CASE column_ordinal
WHEN 1 THEN N'' ELSE N',' END
+ N'<' + name + N', ' +system_type_name + N',>',
N'--', 1000 + column_ordinal
FROM x
UNION ALL
SELECT N'
);', N'--', 10000
ORDER BY column_ordinal;
*/
/*
INSERT [dbo].[table1](
[IDNUM]
,[c1]
,[c2]
,[c3]
,[c7]
) VALUES (
<IDNUM, int,>
,<c1, varchar(9),>
,<c2, int,>
,<c3, int,>
,<c7, numeric(8,2),>
);
*/
Please reply with:
The time of day that you are available. I am available 7am to 10pm UTC-06:00 Central Time US & Canada. Will you require contact by voice and do you speak English? When you can complete the project.
Confidentiality.
a) No Use. Recipient agrees not to use the Confidential Information in any way, or to manufacture or test any product embodying Confidential Information, except for the purpose set forth above.
b) No Disclosure. Recipient agrees to use its best efforts to prevent and protect the Confidential Information, or any part thereof, from disclosure to any person other than Recipient's employees having a need for disclosure in connection with Recipient's authorized use of the Confidential Information.
c) Protection of Secrecy. Recipient agrees to take all steps reasonably necessary to protect the secrecy of the Confidential Information, and to prevent the Confidential Information from falling into the public domain or into the possession of unauthorized persons.
d) Scope. The scope of Confidentiality is deemed to be in all contracts present past and future with the Parties to this request
Related categories:
SQL
Database Administration
Microsoft SQL Server
T-SQL (Transact Structures Query Language)