我将编写一个过程为我生成表:
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;
然后调用:
EXEC p_create_table_from_procedure 'YOUR_TABLE_NAME_HERE', 'YOUR_PROCEDURE_NAME_HERE'
注意:将“ YOUR_PROCEDURE_NAME_HERE”替换为您自己的存储过程的名称。
注意:将YOUR_TABLE_NAME_HERE替换为您选择的表名。
上面的代码将生成如下内容:
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
);