T-SQL存储过程返回表

时间:2013-02-12 03:17:45

标签: sql-server tsql stored-procedures

如何创建一个返回此SELECT语句的(表格)结果的T-SQL存储过程:

USE automation

SELECT insurer.name,
insurer.case_name,
contact.name,
contact_address.line_1,
contact_address.city,
state.state_abbr,
contact_address.zip

FROM person as insurer

INNER JOIN persons_relationship on persons_relationship.person2_id = insurer.person_id
INNER JOIN person contact on contact.person_id = persons_relationship.person_id
INNER JOIN person_address contact_person_address on contact_person_address.person_id = contact.person_id
INNER JOIN address contact_address on contact_address.address_id = contact_person_address.address_id
INNER JOIN state on state.state_id = contact_address.state_id

insurer.person_class_id = 2

3 个答案:

答案 0 :(得分:1)

做这样的事情:

USE Automation
GO

CREATE PROCEDURE dbo.YourProcedureNameHere
AS
   SELECT 
       insurer.name,
       insurer.case_name,
       contact.name,
       contact_address.line_1,
       contact_address.city,
       state.state_abbr,
       contact_address.zip
   FROM 
       person as insurer
   INNER JOIN 
       persons_relationship on persons_relationship.person2_id = insurer.person_id
   INNER JOIN 
       person contact on contact.person_id = persons_relationship.person_id
   INNER JOIN 
       person_address contact_person_address on contact_person_address.person_id = contact.person_id
   INNER JOIN 
       address contact_address on contact_address.address_id = contact_person_address.address_id
   INNER JOIN 
       state on state.state_id = contact_address.state_id

你已经完成了。现在您可以将您的声明称为:

EXEC sp_executesql N'dbo.YourProcedureNameHere'  

答案 1 :(得分:0)

我建议你也放

SET NOCOUNT ON 

直接在 AS 之后。一些数据访问库被 x记录受影响消息混淆,这将其关闭。

答案 2 :(得分:-1)

将此查询放入存储过程中,当您运行此存储过程时,您将获得表格结果