存储过程变量(我相信代码错误存在)

时间:2013-10-08 14:10:23

标签: asp.net sql sql-server vb.net

我正在尝试创建一个脚本来整理SQL Server中的所有列和行(我将做的不仅仅是修剪,但一旦它工作,我可以插入我已经拥有的应用程序代码)

我想我在那里但它似乎没有更新 - 也许存储过程是错误的?我相信错误在这里.. - 我已经查看了存储过程中的变量,并认为它对我来说是正确的。

Insert statements for procedure here

UPDATE systemUsers 
SET @ColumnName = @Update
WHERE ID = @ID 

完整的脚本......

Protected Sub btnTest_Click(sender As Object, e As System.EventArgs) Handles btnTest.Click

        'Select the column names
        Dim cn As SqlConnection = New SqlConnection()
        Dim cmd As SqlCommand = New SqlCommand()
        Dim dr As SqlDataReader
        Dim i As Integer = 0
        cn.ConnectionString = ConfigurationManager.ConnectionStrings("mySQLConnectionString").ConnectionString
        cmd.Connection = cn
        ' this gets the colum name
        cmd.CommandText = "select COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'systemUsers'"

        'Open the connection to the database
        cn.Open()
        ' Execute the sql.
        dr = cmd.ExecuteReader()
        ' Read all of the rows generated by the command (in this case only one row).

        CheckBoxList1.Items.Clear() 'remove all items for the new list

        Do While dr.Read()

            i = i + 1
            Session.Item("ColumnName" & i) = dr.Item("COLUMN_NAME").ToString()
        Loop
        ' Close your connection to the DB.
        dr.Close()
        cn.Close()

        Dim j As Integer = 1
        For j = 1 to i
            cn.ConnectionString = ConfigurationManager.ConnectionStrings("mySQLConnectionString").ConnectionString
            cmd.Connection = cn
            cmd.CommandText = "Select * From [systemUsers]"

            'Open the connection to the database
            cn.Open()
            ' Execute the sql.
            dr = cmd.ExecuteReader()
            ' Read all of the rows generated by the cmd (in this case only one row).
            Dim vName As String = ""
            If vName = "ID" Then
                'skip as ID should never be edited!!
            Else

                Do While dr.Read()

                    vName = Session.Item("ColumnName" & j).ToString ' put into vName for Stored Procedure

                    Dim sConnString As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("mySQLConnectionString").ConnectionString
                    Dim dsNames As SqlDataSource

                    dsNames = New SqlDataSource
                    dsNames.ConnectionString = sConnString
                    Dim sSQL As String
                    sSQL = "SPTidy"

                    dsNames.UpdateCommand = sSQL
                    dsNames.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure
                    dsNames.UpdateParameters.Clear()
                    dsNames.UpdateParameters.Add("ID", dr.Item("ID").ToString())
                    dsNames.UpdateParameters.Add("Update", Trim(dr.Item(vName).ToString()))
                    dsNames.UpdateParameters.Add("ColumnName", vName)
                    dsNames.Update()
                    dsNames = Nothing

                Loop
            End If

            j = j + 1

            ' Close your connection to the DB.
            dr.Close()
            cn.Close()

        Next

    End Sub

存储过程:

create PROCEDURE [dbo].[SPTidy] 
   @ID bigint,
   @Update nvarchar(max),
   @ColumnName nvarchar(max)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    UPDATE systemUsers 
    SET @ColumnName = @Update
    WHERE ID = @ID
END

1 个答案:

答案 0 :(得分:1)

该代码不会抛出错误消息,因为它只是更新变量。要拥有动态列名,您需要使用动态SQL(或复杂的case语句),但在这种情况下,动态SQL更好。

DECLARE @sql nvarchar(max) = '';

SET @sql = N'UPDATE systemUsers SET ' + @ColumnName + N' = ' + @Update + N' WHERE ID=' + @ID

EXEC sp_executesql @sql

希望这会有所帮助:)