Primarily and most importantly, because it's a simple thing to do and a courtesy to others who are hoping to help. To make things even simpler for myself (and others if they care to use it), I've created this script below to be able to quickly create a minimal reproducible example copy of any original table including actual sample data, without having to manually script out the tables and data. Then you can easily change any proprietary info in the results before posting it to a question.
This script uses any original table and generates the following PRINT
statements:
DROP
and CREATE
a temp table with structure matching the original table
INSERT INTO
statement using samples from the actual data of the original table
Just add the original table name into the variable listed, along with the number of sample records required from the table. It will generate all of the statements needed in the Messages window in SSMS. Then just copy and paste those statements into your question, so those answering have something to work with.
I know that you can get similar results in SSMS through Tasks>Generate Scripts, but this script gets things down to the basic minimal amount of code that's useful for posting here without all of the unnecessary info that SSMS generates automatically. It's just a quick way to create a reproduced version of a simple table with actual sample data in it.
Please note: This doesn't work 100% if you run it for very wide tables, but you don't really want that for a minimal reproducible example on SO anyway.
/* ---------------------------------------
-- For creating minimal reproducible examples
-- based on original table and data,
-- builds the following statements
-- -- CREATE temp table with structure matching original table
-- -- INSERT statements based on actual data
--
-- Note: May not work for very wide tables due to limitations on
-- PRINT statements
*/ ---------------------------------------
DECLARE @tableName NVARCHAR(MAX) = 'testTable', -- original table name HERE
@recordCount INT = 5, -- top number of records to insert to temp table
@buildStmt NVARCHAR(MAX),
@insertStmt NVARCHAR(MAX),
@valuesStmt NVARCHAR(MAX),
@insertCol NVARCHAR(MAX),
@strAgg NVARCHAR(MAX),
@insertOutput NVARCHAR(MAX)
IF (EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @tableName))
BEGIN
-- build DROP and CREATE statements for temp table from original table
SET @buildStmt = 'IF OBJECT_ID(''tempdb..#' + @tableName + ''') IS NOT NULL DROP TABLE #' + @tableName + CHAR(10) + CHAR(10) +
'CREATE TABLE #' + @tableName + ' (' + CHAR(10)
SELECT @buildStmt = @buildStmt + ' ' + C.[Name] + ' ' +
T.[Name] +
CASE WHEN T.[Name] IN ('varchar','varchar','char','nchar') THEN '(' + CAST(C.[Length] AS VARCHAR) + ') ' ELSE ' ' END +
'NULL,' + CHAR(10)
FROM sysobjects O
JOIN syscolumns C ON C.id = O.id
JOIN systypes T ON T.xusertype = C.xusertype
WHERE O.[name] = @TableName
ORDER BY C.ColID
SET @buildStmt = SUBSTRING(@buildStmt,1,LEN(@buildStmt) - 2) + CHAR(10) + ')' + CHAR(10)
PRINT @buildStmt
-- build INSERT INTO statement from original table
SELECT @insertStmt = 'INSERT INTO #' + @tableName + ' (' +
STUFF ((
SELECT ', [' + C.[Name] + ']'
FROM sysobjects O
JOIN syscolumns C ON C.id = O.id
WHERE O.[name] = @TableName
ORDER BY C.ColID
FOR XML PATH('')), 1, 1, '')
+')'
PRINT @insertStmt
-- build VALUES portion of INSERT from data in original table
SELECT @insertCol = STUFF ((
SELECT '''''''''+CONVERT(NVARCHAR(200),' +
'[' + C.[Name] + ']' +
')+'''''',''+'
FROM sysobjects O
JOIN syscolumns C ON C.id = O.id
JOIN systypes T ON T.xusertype = C.xusertype
WHERE O.[name] = @TableName
ORDER BY C.ColID
FOR XML PATH('')), 1, 1, '')
SET @insertCol = SUBSTRING(@insertCol,1,LEN(@insertCol) - 1)
SELECT @strAgg = ';WITH CTE AS (SELECT TOP(' + CONVERT(VARCHAR,@recordCount) + ') * FROM ' + @tableName + ') ' +
' SELECT @valuesStmt = STRING_AGG(CAST(''' + @insertCol + ' AS NVARCHAR(MAX)),''), ('') ' +
' FROM CTE'
EXEC sp_executesql @strAgg,N'@valuesStmt NVARCHAR(MAX) OUTPUT', @valuesStmt OUTPUT
PRINT 'VALUES (' +REPLACE(SUBSTRING(@valuesStmt,1,LEN(@valuesStmt) - 1),',)',')') + ')'
END
ELSE
BEGIN
PRINT 'Table does NOT exist'
END