0

我正在尝试创建一个脚本来整理 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
4

1 回答 1

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

希望这可以帮助 :)

于 2013-10-08T14:20:31.340 回答