Using SQL Server 2012. I am aware of the ability in SSMS to script schema & data to a query window, file, etc.
Right click on database, Tasks --> Generate Scripts --> Choose Objects --> Advanced Scripting Options --> General --> Types of data to script --> Schema and Data (Schema Only/Data Only)
Is there a way to do this on a table variable or #temp table without using a third-party product? (and/or script the Schema, Data, Schema and Data, as in the Scripting menu).
I was able to do this using a third-party product, in which I ran the query to select to grid, and then scripted the results. But, I'd like to be able to do this without this product if possible.
My test query:
/*
CREATE VIEW dbo.vRandomNumber
AS
SELECT RAND() AS RandomNumber
GO
CREATE FUNCTION dbo.UDF_Q_RandomNumber()
RETURNS NUMERIC(18, 10)
AS
BEGIN
RETURN (SELECT TOP 1 RandomNumber FROM vRandomNumber)
END
GO
*/
declare @t_testTable TABLE (
[nbr] int,
[InfoField] [nvarchar](max) NULL,
[AnotherInfoField] [nvarchar](max) NULL,
[description] [nvarchar](max) NULL,
[Rnum] int,
[TransDate] datetime
);
declare @v_dt datetime;
set @v_dt = getdate();
insert into @t_testTable (
[nbr],
[InfoField],
[AnotherInfoField],
[description],
[Rnum],
[TransDate])
VALUES (1,'Info1','Info 1 Field','Test Field - info 1', cast(1000 * dbo.UDF_Q_RandomNumber()+1 AS INT),@v_dt),
(2,'Info2','Info 2 Field','Test Field - info 2', cast(1000 * dbo.UDF_Q_RandomNumber()+1 AS INT),@v_dt),
(3,'Info3','Info 3 Field','Test Field - info 3', cast(1000 * dbo.UDF_Q_RandomNumber()+1 AS INT),@v_dt),
(4,'Info4','Info 4 Field','Test Field - info 4', cast(1000 * dbo.UDF_Q_RandomNumber()+1 AS INT),@v_dt),
(5,'Info5','Info 5 Field','Test Field - info 5', cast(1000 * dbo.UDF_Q_RandomNumber()+1 AS INT),@v_dt),
(6,'Info6','Info 6 Field','Test Field - info 6', cast(1000 * dbo.UDF_Q_RandomNumber()+1 AS INT),@v_dt),
(7,'Info7','Info 7 Field','Test Field - info 7', cast(1000 * dbo.UDF_Q_RandomNumber()+1 AS INT),@v_dt),
(8,'Info8','Info 8 Field','Test Field - info 8', cast(1000 * dbo.UDF_Q_RandomNumber()+1 AS INT),@v_dt),
(9,'Info9','Info 9 Field','Test Field - info 9', cast(1000 * dbo.UDF_Q_RandomNumber()+1 AS INT),@v_dt),
(10,'Info10','Info 10 Field','Test Field - info 10', cast(1000 * dbo.UDF_Q_RandomNumber()+1 AS INT),@v_dt);
select * from @t_testTable