将值插入DateTime字段中的动态SQL

时间:2019-02-08 16:31:26

标签: sql-server vb.net

我正在此SQL Server存储过程中使用动态SQL编写一条Insert into语句:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spTransaction]
    @UpdateType             Int,
    @BillCode               Char(17),
    @PatientName            Varchar(max),
    @MobileNo               Varchar(20),
    @Address                Varchar(max),
    @BillDate               DateTime,
    @BedFrom                Date,
    @BedTo                  Date,
    @OTType                 Char(3),
    @OTMedicineCharge       Decimal(18,2),
    @WardMedicineCharge     Decimal(18,2),
    @MonitorUsed            Int,
    @OxygenUsed             Int,
    @PulltionUsed           Int,
    @ECGUsed                Int,
    @PathologyCharge        Decimal(18,2),
    @DressingType           Char(3),
    @NebuligerUsed          Int,
    @DoctorFees             Decimal(18,2),
    @AnaesthisistCharge     Decimal(18,2),
    @AsstOfScFees           Decimal(18,2),
    @AttendentTime          Int,
    @OtherChargesCode       Char(5),
    @AcYr                   Char(4),

    @Error Varchar(50)      Output,
    @OutBillCode Char(17)   Output
AS
BEGIN
    IF DATEDIFF (Day, GETDATE(), @BillDate) < 0
    BEGIN
        SET @Error = 'Backward Entry Date is not permitted'
        RETURN
    END

    IF LEN(@OtherChargesCode) = 0
    BEGIN
        SET @OtherChargesCode = '00000'
    END

    DECLARE @temptableName CHAR(21)
    DECLARE @sql NVARCHAR(MAX)

    DECLARE @tempBillNo CHAR(7)
    DECLARE @tempBedCharge DECIMAL(18,2)
    DECLARE @OTCharge Decimal(18,2)
    DECLARE @MonitorCharge Decimal(18,2)
    DECLARE @OxygenCharge Decimal(18,2)
    DECLARE @PulltionCharge Decimal(18,2)
    DECLARE @ECGCharge Decimal(18,2)
    DECLARE @DressingCharge Decimal(18,2)
    DECLARE @NebuligerCharge Decimal(18,2)
    DECLARE @AttendentCharge Decimal(18,2)
    DECLARE @OtherCharge Decimal(18,2)
    DECLARE @TotalCharge Decimal (18,2)
    DECLARE @GrandTotal Decimal(18,2)

    Set @tempBedCharge = (DATEDIFF(Day,@BedFrom,@BedTo) + 1)*(Select BedCharge from ChargesMast)
    Set @OTCharge = (Select OTCharge from OTTypeMast Where OTType = @OTType)
    Set @MonitorCharge = (Select MonitorCharge From ChargesMast)*@MonitorUsed
    Set @OxygenCharge = (Select OxygenCharge From ChargesMast)*@OxygenUsed
    Set @PulltionCharge = (Select PulltionCharge From ChargesMast)*@PulltionUsed
    Set @ECGCharge = (Select ECGCharge From ChargesMast)*@ECGUsed
    Set @DressingCharge = (Select DressingCharge From DressingTypeMast Where DressingType = @DressingType)
    Set @NebuligerCharge = (Select NebuligerCharge From ChargesMast)*@NebuligerUsed
    Set @AttendentCharge = (Select AttendentCharge From ChargesMast)*@AttendentTime

    Set @TotalCharge = @tempBedCharge + @OTCharge + @OTMedicineCharge + @WardMedicineCharge + @MonitorCharge + @OxygenCharge
                            + @PulltionCharge + @ECGCharge + @PathologyCharge + @DressingCharge + @NebuligerCharge + @DoctorFees
                            + @AnaesthisistCharge + @AsstOfScFees + @AttendentCharge

    Set @OtherCharge = (Select isNULL((select ChargeValue From OtherChargesMast Where OtherChargeCode = @OtherChargesCode),0))

    Set @GrandTotal = @OtherCharge + @TotalCharge

    Set @temptableName = 'TransactionDetail' + @AcYr
    If @UpdateType = 1
    Begin
        Set @tempBillNo = (Select Right('00000' + Cast(isNull(MAX(BillNo),00000) + 1 as varchar),5)  from TransactionDetail1819)
        Set @OutBillCode = 'BILL/' + @AcYr + '/' + 'N'+ '/' + @tempBillNo 

        Set @sql = 'Insert into ' + @temptableName + '(BillCode,BillNo,PatientName,MobileNo,Address,BillDate,BedFrom,BedTo,BedCharge,OTType,
                                        OTCharge,OTMedicineCharge,WardMedicineCharge,MonitorUsed,MonitorCharge,OxygenUsed,OxygenCharge,PulltionUsed,
                                        PulltionCharge,ECGUsed,ECGCharge,PathologyCharge,DressingType,DressingCharge,NebuligerUsed,
                                        NebuligerCharge,DoctorFees,AnaesthisistCharge,AsstOfScFees,AttendentTime,AttendentCharge,
                                        Total,OtherChargesCode,GrandToTal)

                            values(' + @OutBillCode +',' + @tempBillNo +','+ @PatientName +',' + @MobileNo +',' +@Address+',' + @BillDate + ',' + @BedFrom +',' + @BedTo +',' +@tempBedCharge+',' +
                            @OTType + ',' + @OTCharge +',' + @OTMedicineCharge + ',' + @WardMedicineCharge + ',' + @MonitorUsed + ',' +@MonitorCharge +',' + @OxygenUsed +',' +@OxygenCharge +',' +
                            @PulltionUsed +',' + @PulltionCharge+ ',' + @ECGUsed + ',' + @ECGCharge + ',' + @PathologyCharge + ',' + @DressingType + ',' + @DressingCharge + ',' + @NebuligerUsed + ',' +
                            @NebuligerCharge + ',' + @DoctorFees +',' + @AnaesthisistCharge + ',' +@AsstOfScFees+ ',' + @AttendentTime + ',' + @AttendentCharge + ',' +@TotalCharge + ',' +
                            @OtherChargesCode + ',' + @GrandTotal + ')'
        EXEC sp_executesql @sql


    End



    If @UpdateType = 3
    Begin
        EXEC('Delete @temptableName Where BillCode = @BillCode')
        Set @OutBillCode = @BillCode
    End
End

并将值传递为:

Private Sub Data_Manipulate(ByVal updateType As Integer)
        If Update_Status() = True Then
            Try
                sqlCon = New SqlConnection(myDataClass.myConnectionString)
                sqlCmd.Connection = sqlCon
                sqlCmd.CommandText = "spTransaction"
                sqlCmd.CommandType = CommandType.StoredProcedure

                sqlCmd.Parameters.Clear()
                sqlCmd.Parameters.Add("UpdateType", SqlDbType.Int).Value = updateType
                sqlCmd.Parameters.Add("BillCode", SqlDbType.Char).Value = txtBillCode.Text
                sqlCmd.Parameters.Add("PatientName", SqlDbType.VarChar).Value = txtName.Text
                sqlCmd.Parameters.Add("MobileNo", SqlDbType.VarChar).Value = txtMobile.Text
                sqlCmd.Parameters.Add("Address", SqlDbType.VarChar).Value = txtAddress.Text
                sqlCmd.Parameters.Add("BillDate", SqlDbType.DateTime).Value = dTPDate.Value
                sqlCmd.Parameters.Add("BedFrom", SqlDbType.Date).Value = dTPBedFrom.Value
                sqlCmd.Parameters.Add("BedTo", SqlDbType.Date).Value = dTPBedTo.Value
                sqlCmd.Parameters.Add("OTType", SqlDbType.Char).Value = cboOTType.SelectedValue
                sqlCmd.Parameters.Add("OTMedicineCharge", SqlDbType.Decimal).Value = Val(txtOTMedicineCharge.Text)
                sqlCmd.Parameters.Add("WardMedicineCharge", SqlDbType.Decimal).Value = Val(txtWardMedicineCharge.Text)
                sqlCmd.Parameters.Add("MonitorUsed", SqlDbType.Int).Value = Val(txtMonitorUsed.Text)
                sqlCmd.Parameters.Add("OxygenUsed", SqlDbType.Int).Value = Val(txtOxygenUsed.Text)
                sqlCmd.Parameters.Add("PulltionUsed", SqlDbType.Int).Value = Val(txtPulltionUsed.Text)
                sqlCmd.Parameters.Add("ECGUsed", SqlDbType.Int).Value = Val(txtECGUsed.Text)
                sqlCmd.Parameters.Add("PathologyCharge", SqlDbType.Decimal).Value = Val(txtPathologyCharge.Text)
                sqlCmd.Parameters.Add("DressingType", SqlDbType.Char).Value = cboDressingType.SelectedValue
                sqlCmd.Parameters.Add("NebuligerUsed", SqlDbType.Int).Value = Val(txtNebuligerUsed.Text)
                sqlCmd.Parameters.Add("DoctorFees", SqlDbType.Decimal).Value = Val(txtdoctorFees.Text)
                sqlCmd.Parameters.Add("AnaesthisistCharge", SqlDbType.Decimal).Value = Val(txtAnaesthisist.Text)
                sqlCmd.Parameters.Add("AsstOfScFees", SqlDbType.Decimal).Value = Val(txtAssistantOfScFees.Text)
                sqlCmd.Parameters.Add("AttendentTime", SqlDbType.Int).Value = Val(txtAttendent.Text)
                sqlCmd.Parameters.Add("OtherChargesCode", SqlDbType.Char).Value = txtOtherChargeCode.Text
                sqlCmd.Parameters.Add("AcYr", SqlDbType.Char).Value = mAcYr

                sqlCmd.Parameters.Add("@Error", SqlDbType.VarChar, 50)
                sqlCmd.Parameters("@Error").Direction = ParameterDirection.Output

                sqlCmd.Parameters.Add("@OutBillCode", SqlDbType.Char, 17)
                sqlCmd.Parameters("@OutBillCode").Direction = ParameterDirection.Output

                If sqlCon.State = ConnectionState.Closed Then
                    sqlCon.Open()
                End If
                sqlCmd.ExecuteNonQuery()

                If Len(sqlCmd.Parameters("@Error").Value.ToString()) <> 0 Then
                    MessageBox.Show(sqlCmd.Parameters("@Error").Value.ToString(), mAppName, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    dTPDate.Select()
                    If sqlCon.State = ConnectionState.Open Then
                        sqlCon.Close()
                    End If
                    Exit Sub
                End If

                txtBillCode.Text = sqlCmd.Parameters("@OutBillCode").Value.ToString()

                btnPrint.Visible = True
            Catch ex As Exception
                MessageBox.Show(ex.Message, mAppName, MessageBoxButtons.OK, MessageBoxIcon.Error)
                Reset_Text()
            Finally
                sqlCon.Close()

            End Try
            Set_Buttons(True)
        End If
    End Sub

但是提示我错误:从字符串转换日期和/或时间时转换失败。

如何传递日期字段以避免错误?

我尝试了所有参考,但没有成功。

1 个答案:

答案 0 :(得分:1)

这是 unested ,但是,我怀疑这将使您找到合适的位置。如果仍然无法解决问题,那么我怀疑scope.arel.where_sql.gsub(/\AWHERE /i, "")中列的顺序不会使INSERT子句中提供的值无效。我没有检查过,但是VALUES / PRINT @SQL将在这里为您提供帮助。

无论如何,这里我已经参数化了您的SQL,并认为我的数据类型正确,所以希望可以完成工作:

SELECT