有没有办法定义临时表而不预先定义它的架构?
实际上使用表VARIABLE(内存表)是最佳选择.#table在临时数据库中创建一个表,##表是全局的 - 都有磁盘命中.考虑交易数量经历的减速/打击.
CREATE PROCEDURE [dbo].[GetAccounts] @AccountID BIGINT, @Result INT OUT, @ErrorMessage VARCHAR(255) OUT AS BEGIN SET NOCOUNT ON; SET @Result = 0 SET @ErrorMessage = '' DECLARE @tmp_Accounts TABLE ( AccountId BIGINT, AccountName VARCHAR(50), ... ) INSERT INTO @tmp_Accounts ([AccountId], [AccountName]... ) SELECT AccountID, AccountName FROM Accounts WHERE ... IF @@Rowcount = 0 BEGIN SET @ErrorMessage = 'No accounts found.' SET @Result = 0 RETURN @Result END ELSE BEGIN SET @Result = 1 SELECT * FROM @tmp_Accounts END
请注意插入此临时表的方式.
这样做的缺点是写入可能需要更长的时间,因为您必须定义表变量.
我还推荐RedGate的SQL Prompt for Query Analyzer.
你不需要OPENQUERY.只需在选择列表和任何查询的FROM之间放入"INTO #AnyTableName"...
SELECT * INTO #Temp1 FROM table1 WHERE x=y
是的,你可以创建它
SELECT INTO ...
让我们说吧
SELECT * INTO #t FROM OPENQUERY( 'server', 'exec database.dbo.proc_name value1, value2, ... ' )