MS SQL: How to Assign A Table Column to a Variable



  • I'm trying to assign a calculation involving two table columns as the value of a variable. I keep getting the error that the multi-part identifier could not be bound. My ultimate objective is to recreate the variable in a stored procedure because I would like to use that variable in other calculations in the same stored procedure.

    My query is below.

    USE dbAttendanceHR
    GO
    

    DECLARE @WorkDuration AS DECIMAL

    SET @WorkDuration = CAST(DATEDIFF(minute, dbo.tblAttendance.ClockInTime, dbo.tblAttendance.ClockOutTime) AS FLOAT) / 60

    SELECT @WorkDuration

    The error message are...

    Msg 4104, Level 16, State 1, Line 5 The multi-part identifier "dbo.tblAttendance.ClockInTime" could not be bound.

    Msg 4104, Level 16, State 1, Line 5 The multi-part identifier "dbo.tblAttendance.ClockOutTime" could not be bound.

    Please help. Thanks.



  • Does this fit your requests?

    USE dbAttendanceHR
    GO 
    

    DECLARE @WorkDuration AS NVARCHAR(max)

    SET @WorkDuration = 'SELECT ' + 'CAST(DATEDIFF(minute, dbo.tblAttendance.ClockInTime, dbo.tblAttendance.ClockOutTime) AS FLOAT) / 60'

    EXECUTE sp_executesql @WorkDuration




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2