Saya akan menulis prosedur untuk menghasilkan tabel untuk saya:
CREATE PROCEDURE [dbo].[p_create_table_from_procedure]
@TABLE_NAME AS NVARCHAR(MAX),
@PROCEDURE_NAME AS NVARCHAR(MAX)
As
DECLARE @CREATE_TABLE_QUERY NVARCHAR(MAX) = N'';
SELECT
@CREATE_TABLE_QUERY += ', ' + name + ' ' + UPPER(system_type_name) + CHAR(13) + CHAR(10) + CHAR(9)
FROM
sys.dm_exec_describe_first_result_set(@procedure_name, NULL, 1);
SELECT
@CREATE_TABLE_QUERY = N'CREATE TABLE ' + @table_name + '(' + CHAR(13) + CHAR(10) + CHAR(9) + STUFF(@CREATE_TABLE_QUERY, 1, 1, N'') + ');';
PRINT @CREATE_TABLE_QUERY;
Kemudian sebut dengan:
EXEC p_create_table_from_procedure 'YOUR_TABLE_NAME_HERE', 'YOUR_PROCEDURE_NAME_HERE'
Catatan : Ganti 'YOUR_PROCEDURE_NAME_HERE' dengan nama prosedur tersimpan Anda sendiri.
Catatan : Ganti YOUR_TABLE_NAME_HERE dengan nama tabel pilihan Anda.
Di atas akan menghasilkan sesuatu seperti ini:
CREATE TABLE YOUR_TABLE_NAME_HERE(
WeekName VARCHAR(40)
, Line Name VARCHAR(50)
, TheDate DATETIME
, ReceivedAll INT
, Answered INT
, Abandoned INT
, Call Length INT
, WaitTimeAnswer INT
, WaitTimeAbandon INT
, PeriodName VARCHAR(10)
, Week SMALLINT
, Period SMALLINT
, Year SMALLINT
, WeekInPeriod SMALLINT
, NumWeeksInPeriod SMALLINT
, WeekendDate DATETIME
, CRCOperative VARCHAR(100)
, CallType VARCHAR(20)
, Charge Time INT
, SourceNumber VARCHAR(80)
, DestinationNumber VARCHAR(80)
, CallStart DATETIME
, Out of Hours VARCHAR(12)
, IsWorkingDay BIT
);