<tfoot id='jyi8T'></tfoot>
      <bdo id='jyi8T'></bdo><ul id='jyi8T'></ul>

    <small id='jyi8T'></small><noframes id='jyi8T'>

  1. <i id='jyi8T'><tr id='jyi8T'><dt id='jyi8T'><q id='jyi8T'><span id='jyi8T'><b id='jyi8T'><form id='jyi8T'><ins id='jyi8T'></ins><ul id='jyi8T'></ul><sub id='jyi8T'></sub></form><legend id='jyi8T'></legend><bdo id='jyi8T'><pre id='jyi8T'><center id='jyi8T'></center></pre></bdo></b><th id='jyi8T'></th></span></q></dt></tr></i><div id='jyi8T'><tfoot id='jyi8T'></tfoot><dl id='jyi8T'><fieldset id='jyi8T'></fieldset></dl></div>

    1. <legend id='jyi8T'><style id='jyi8T'><dir id='jyi8T'><q id='jyi8T'></q></dir></style></legend>

      我可以在存储过程中有一个可选的 OUTPUT 参数吗?

      时间:2023-10-26
      <i id='88UB4'><tr id='88UB4'><dt id='88UB4'><q id='88UB4'><span id='88UB4'><b id='88UB4'><form id='88UB4'><ins id='88UB4'></ins><ul id='88UB4'></ul><sub id='88UB4'></sub></form><legend id='88UB4'></legend><bdo id='88UB4'><pre id='88UB4'><center id='88UB4'></center></pre></bdo></b><th id='88UB4'></th></span></q></dt></tr></i><div id='88UB4'><tfoot id='88UB4'></tfoot><dl id='88UB4'><fieldset id='88UB4'></fieldset></dl></div>

      <small id='88UB4'></small><noframes id='88UB4'>

      <tfoot id='88UB4'></tfoot>
      <legend id='88UB4'><style id='88UB4'><dir id='88UB4'><q id='88UB4'></q></dir></style></legend>

            <tbody id='88UB4'></tbody>

              • <bdo id='88UB4'></bdo><ul id='88UB4'></ul>

              • 本文介绍了我可以在存储过程中有一个可选的 OUTPUT 参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个存储过程,它有一堆输入和输出参数,因为它正在向多个表插入值.在某些情况下,存储过程仅插入到单个表中(取决于输入参数).这是一个模拟场景来说明.

                I have a stored procedure that has a bunch of input and output parameters because it is Inserting values to multiple tables. In some cases the stored proc only inserts to a single table (depending on the input parameters). Here is a mocked up scenario to illustrate.

                表格/数据对象:

                Id
                Name
                Address
                

                姓名

                Id
                FirstName
                LastName
                

                地址

                Id
                Country
                City
                

                假设我有一个插入一个人的存储过程.如果地址不存在,我不会将其添加到数据库中的 Address 表中.

                Say I have a stored procedure that inserts a person. If the address doesn't exist I won't add it to the Address table in the database.

                因此,当我生成代码来调用存储过程时,我不想费心添加Address 参数.对于 INPUT 参数,这是可以的,因为 SQL Server 允许我提供默认值.但是对于 OUTPUT 参数,我在存储过程中如何使其成为可选参数,这样我就不会收到错误...

                Thus when I generate the code to call the stored procedure I don't want to bother adding the Address parameter. For INPUT parameters this is ok because SQL Server allows me to supply default values. But for the OUTPUT parameter what do I do in the stored procedure to make it optional so I do not receive an error...

                过程或函数Person_InsertPerson"需要参数'@AddressId',未提供.

                推荐答案

                输入和输出参数都可以指定默认值.在这个例子中:

                Both input and output parameters can be assigned defaults. In this example:

                CREATE PROCEDURE MyTest
                  @Data1 int
                 ,@Data2 int = 0
                 ,@Data3 int = null output
                
                AS
                
                PRINT @Data1
                PRINT @Data2
                PRINT isnull(@Data3, -1)
                
                SET @Data3 = @Data3 + 1
                
                RETURN 0
                

                第一个参数是必需的,第二个和第三个参数是可选的——如果调用例程没有设置,它们将被分配默认值.尝试使用不同的值和设置在 SSMS 中处理它和以下测试调用例程,以查看它们如何协同工作.

                the first paramter is required, and the second and third are optional--if not set by the calling routine, they will be assigned the default values. Try messing around with it and the following test-call routine in SSMS using different values and settings to see how it all works together.

                DECLARE @Output int
                
                SET @Output = 3
                
                EXECUTE MyTest
                  @Data1 = 1
                 ,@Data2 = 2
                 ,@Data3 = @Output output
                
                PRINT '---------'
                PRINT @Output
                

                这篇关于我可以在存储过程中有一个可选的 OUTPUT 参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 SQL 存储过程中实现日志记录和错误报告? 下一篇:如何在存储过程中使用插入删除表?

                相关文章

              • <i id='SAuV7'><tr id='SAuV7'><dt id='SAuV7'><q id='SAuV7'><span id='SAuV7'><b id='SAuV7'><form id='SAuV7'><ins id='SAuV7'></ins><ul id='SAuV7'></ul><sub id='SAuV7'></sub></form><legend id='SAuV7'></legend><bdo id='SAuV7'><pre id='SAuV7'><center id='SAuV7'></center></pre></bdo></b><th id='SAuV7'></th></span></q></dt></tr></i><div id='SAuV7'><tfoot id='SAuV7'></tfoot><dl id='SAuV7'><fieldset id='SAuV7'></fieldset></dl></div>
                  <bdo id='SAuV7'></bdo><ul id='SAuV7'></ul>

              • <tfoot id='SAuV7'></tfoot><legend id='SAuV7'><style id='SAuV7'><dir id='SAuV7'><q id='SAuV7'></q></dir></style></legend>
                  1. <small id='SAuV7'></small><noframes id='SAuV7'>