将参数传递给 Updategram (SQLXML 4.0)

适用于: SQL Server Azure SQL 数据库

Updategram 为模板,因此您可以向其传递参数。 有关将参数传递给模板的详细信息,请参阅 Updategram 安全注意事项(SQLXML 4.0)。

Updategram 允许您将 NULL 作为参数值传递。 若要传递 NULL 参数值,请指定 nullvalue 属性。 然后将分配给 nullvalue 属性的值作为参数值提供。 Updategram 将该值视为 NULL。

注意

<sql:header><updg:header> 中,应将 nullvalue 指定为 unqualified;而在 updg:sync><,应将 nullvalue 指定为限定值(例如 updg:nullvalue)。

示例

若要使用以下示例创建工作示例,必须满足运行 SQLXML 示例的要求中指定的要求。

在使用 updategram 示例前,请注意以下事项:

A. 将参数传递给 updategram

在本示例中,updategram 更改了 HumanResources.Shift 表中一位雇员的姓氏。 向 updategram 传递了两个参数:ShiftID(用于唯一标识轮班)和 Name。

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
<updg:header>  
  <updg:param name="ShiftID"/>  
  <updg:param name="Name" />  
</updg:header>  
  <updg:sync >  
    <updg:before>  
       <HumanResources.Shift ShiftID="$ShiftID" />  
    </updg:before>  
    <updg:after>  
      <HumanResources.Shift Name="$Name" />  
    </updg:after>  
  </updg:sync>  
</ROOT>  
测试 updategram
  1. 将上面的 updategram 复制到记事本中并将其另存为 UpdategramWithParameters.xml 文件。

  2. 在“使用 ADO 执行 SQLXML 4.0 查询”中准备 SQLXML 4.0 测试脚本(Sqlxml4test.vbs),以便在以下行后面cmd.Properties("Output Stream").Value = outStream添加以下行来执行更新报:

    cmd.NamedParameters = True  
    ' CreateParameter arguments: Name, Type, Direction, Size, Value  
    cmd.Parameters.Append cmd.CreateParameter("@ShiftID",  2, 1,  0, 1)  
    cmd.Parameters.Append cmd.CreateParameter("@Name",   200, 1, 50, "New Name")  
    

B. 将 NULL 作为参数值传递给 updategram

在执行 updategram 的过程中,会将“isnull”值赋给要设置为 NULL 的参数。 Updategram 将“isnulll”参数值转换为 NULL 并对其进行相应处理。

以下 updategram 将员工标题设置为 NULL:

<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-updategram">  
<updg:header nullvalue="isnull" >  
  <updg:param name="EmployeeID"/>  
  <updg:param name="ManagerID" />  
</updg:header>  
  <updg:sync >  
    <updg:before>  
       <HumanResources.Employee EmployeeID="$EmployeeID" />  
    </updg:before>  
    <updg:after>  
      <HumanResources.Employee ManagerID="$ManagerID" />  
    </updg:after>  
  </updg:sync>  
</ROOT>  
测试 updategram
  1. 将上面的 updategram 复制到记事本中并将其另存为 UpdategramPassingNullvalues.xml 文件。

  2. 在“使用 ADO 执行 SQLXML 4.0 查询”中准备 SQLXML 4.0 测试脚本(Sqlxml4test.vbs),以便在以下行后面cmd.Properties("Output Stream").Value = outStream添加以下行来执行更新报:

    cmd.NamedParameters = True  
    ' CreateParameter arguments: Name, Type, Direction, Size, Value   
    cmd.Parameters.Append cmd.CreateParameter("@EmployeeID", 3, 1, 0, 1)  
    cmd.Parameters.Append cmd.CreateParameter("@ManagerID",  3, 1, 0, Null)  
    

另请参阅

updategram 安全注意事项 (SQLXML 4.0)