Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9192023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:49:42+00:00 2026-06-17T20:49:42+00:00

How can I create a table-valued function from this query? I need to calculate

  • 0

How can I create a table-valued function from this query?

I need to calculate time as result HH:MM between start and end job time

This query work when I run it in SQL :

DECLARE @USERID int;
SET @USERID = 10

DECLARE @JOBStartDATE DATETIME;
SET @JOBStartDATE = (SELECT StartJOBHoursDATE FROM JOBs WHERE ID=@USERID) 


DECLARE @StartTime DATETIME;
DECLARE @JOBDateTime DATETIME;
DECLARE @JOBEvent nvarchar(50);
DECLARE @totalTime int;
SET @totalTime = 0;

SELECT  ROW_NUMBER() OVER(ORDER BY JOBID) AS ROWNUM, JOBDateTime,JOBEvent  INTO #TEMP FROM  JOBsActivityData where JOBID = @USERID and JOBDateTime >= @JOBStartDATE
DECLARE @MaxRownum INT
SET @MaxRownum = (SELECT MAX(RowNum) FROM #TEMP)
DECLARE @Iter INT
SET @Iter = (SELECT MIN(RowNum) FROM #TEMP)

WHILE @Iter <= @MaxRownum
BEGIN
SET @JOBDateTime =(SELECT JOBDateTime FROM #TEMP WHERE RowNum = @Iter)
SET @JOBEvent =(SELECT JOBEvent FROM #TEMP WHERE RowNum = @Iter)
IF(@JOBEvent = 'START')
BEGIN
SET @StartTime =(SELECT JOBDateTime FROM #TEMP WHERE RowNum = @Iter)
END
IF(@JOBEvent = 'END' AND @StartTime IS NOT NULL)
BEGIN
SET @totalTime = @totalTime + (SELECT DATEDIFF(minute,@StartTime,@JOBDateTime))
SET @StartTime = NULL;
END

    SET @Iter = @Iter + 1
END

DROP TABLE #TEMP

SELECT CAST((@totalTime / 60) AS VARCHAR(8)) + ':' + 
       CAST((@totalTime % 60) AS VARCHAR(2)) AS JOBHours

When I try to create I get this error

Cannot access temporary tables from within a function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:49:43+00:00Added an answer on June 17, 2026 at 8:49 pm

    The server does not allow modification of any table in a function. Use a table variable instead.

    declare @temp table (RowNum int, JOBDateTime DateTime, JOBEvent int)
    
    insert into @temp
      SELECT ROW_NUMBER() OVER(ORDER BY JOBID) AS ROWNUM, 
             JOBDateTime,
             JOBEvent  
        FROM JOBsActivityData 
       where JOBID = @USERID and JOBDateTime >= @JOBStartDATE
    ...
    

    when using table variables, you do not need to drop them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a view or a table valued function that returns one
Can we create a temporary table from the stored procedure results dynamically? I do
Can I create a Hive table from a sql/text file like we do in
Can I fire show create table query in Java code for mysql? What I
Any ideas how I can create a join on a table retrieved from data
How can i get result My Dream Table Result CREATE TABLE #temp( [count] int
You can create a table model and add it to a table TableModel tm
Can I create a table without any columns in SQL Server by t-sql?
Can i create a table by using only a UITableView without a UITableViewController? Is
How can I create a table with autoincrement in access. Here is what I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.