473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Getting the last inserted row by smalldatetime

17
I am creating a Stored Proc and I need to be able to select the last added row, now this should be made easier by the fact that I have a smalldatetime column in the table that is added every time a new row is inserted. I use MSSQL 2005 and the construction of the table is this:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE temp_sensor_table
  2.     (    
  3.     temp_id Int Identity PRIMARY KEY,
  4.     device_id Int NOT NULL
  5.         REFERENCES Devices(device_id),
  6.     temp_value NVARChar(10) NOT NULL,
  7.     temp_date smalldatetime NOT NULL DEFAULT GETDATE()
  8.     )
  9.  
And the select statement that i have is this:
Expand|Select|Wrap|Line Numbers
  1. select temp_value 
  2. from temp_sensor_table 
  3. where device_id = @device_id_temp 
  4. and temp_date = ???
  5.  
The @device_id_temp is just an int that is selected in another select statement in the Stored Proc, what I need is the last part of the where statement.

NormannTheDane
Sep 13 '07 #1
4 3842
almaz
168 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. ;with Ordered(temp_value, Position) as
  2. (
  3.     select temp_value, Position = row_number() over(order by temp_date desc)
  4.     from temp_sensor_table 
  5.     where device_id = @device_id_temp 
  6. )
  7. select temp_value 
  8. from Ordered 
  9. where Position = 1
Sep 13 '07 #2
Normann
17
The select statement from above works perfectly, if it is on it's own but when I try to use it in the Stored Proc I get an error.

I think it is when I try to use it to set a parameter, but I might be wrong. I have include what I am trying to do, and I hope someone can spot what I am doing wrong.

Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE dbo.SP_Get_Data_Status
  2. (
  3. @room_name nvarchar(20),            --Input vari from C#
  4. @temp_value nvarchar(4) output,            --Output vari to C#    
  5. @humid_value nvarchar(4) output            --Output vari to C#
  6. )
  7. AS
  8.     DECLARE @room_id int            
  9.     DECLARE @device_type_id_temp int        
  10.     DECLARE @device_type_id_humid int    --Vari used in 
  11.     DECLARE @device_type_id_fire int       --the Stored Proc
  12.     DECLARE @device_id_temp int        
  13.     DECLARE @device_id_humid int        
  14.  
  15.  
  16. SET @room_id = (                    --Gets the room ID 
  17. select room_id from Rooms            --from Rooms Table
  18. where room_name = @room_name)        --using @room_name
  19.  
  20. SET @device_type_id_temp = (            --Gets the type ID 
  21. select device_type_id from Device_Type          --from Device_Type Table
  22. where device_type_name = 'temp')        --using @room_id
  23.  
  24. SET @device_type_id_humid = (            --Gets the type ID
  25. select device_type_id from Device_Type           --from Device_Type Table
  26. where device_type_name = 'humid')        --using @room_id
  27.  
  28. SET @device_id_temp = (                       --Gets the device ID
  29. select device_id from Devices                --from Devices Table
  30. where room_id = @room_id and            --using @room_id &
  31. device_type_id = @device_type_id_temp)        --@device_type_id_temp
  32.  
  33. SET @device_id_humid = (              --Gets the device ID
  34. select device_id from Devices                --from Devices Table
  35. where room_id = @room_id and            --using @room_id &
  36. device_type_id = @device_type_id_humid)        --@device_type_id_humid
  37.  
  38.  
  39. --Code from TheScripts.com forum By almaz Thread_id: 707431
  40. ;with Ordered(temp_value, Position) as        
  41. (
  42. select temp_value, Position = row_number() 
  43. over(order by temp_date desc)
  44. from temp_sensor_table
  45. where device_id = '1'
  46. )
  47. SET @temp_value = (    --<-- Error happens here
  48. select temp_value
  49. from Ordered
  50. where Position = 1)
  51.  

When I try to execute it the error I recive is:

Msg 156, Level 15, State 1, Procedure SP_Get_Data_Status, Line 45
Incorrect syntax near the keyword 'SET'.
Sep 13 '07 #3
almaz
168 Expert 100+
;with Ordered(temp_value, Position) as
(
select temp_value, Position = row_number()
over(order by temp_date desc)
from temp_sensor_table
where device_id = '1'
)
select @temp_value = temp_value
from Ordered
where Position = 1
Sep 14 '07 #4
Normann
17
Thank you very much for your help, it is working now

NormannTheDane
Sep 14 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Robbie | last post by:
Hi All This is a belter that my little brain can't handle. Basically I have 1 SQL table that contains the following fields: Stock Code Stock Desc Reference Transaction Date
2
by: ImraneA | last post by:
Hi there Application : Access v2K/SQL 2K Jest : Using sproc to append records into SQL table Jest sproc : 1.Can have more than 1 record - so using ';' to separate each line from each other.
3
by: siatki | last post by:
Hello, Firstly, sorry for my english. I have problem with creating SQL statement. I am beginner and I think that it is very easy to do. Look - I have to get only the last 20 records from table...
5
by: Ray via SQLMonster.com | last post by:
Hi there, See if you can help me with the following: I need to write an SQL code that will return me: The 1st day & the Last day of the Previous Month in the following format...
2
by: mgarriss | last post by:
Given this table: CREATE TABLE test ( id SERIAL, example TEXT ); An implicit sequence is created as show in this message: NOTICE: CREATE TABLE will create implicit sequence 'test_id_seq'...
3
by: Mark | last post by:
I'm using ASP.Net to accress a database, what I need to do is get the fields out of the very last record in the db. How do I do this? Actually I'm after the primary key, titled 'AdID' it'll tell...
13
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the...
6
by: SQL Server | last post by:
I've been working this for a while. Kind of new to SQL Server functions and not seeing what I am doing wrong. I have this function CREATE FUNCTION dbo.test (@Group varchar(50)) RETURNS...
1
davydany
by: davydany | last post by:
Hey guys...a n00b Here for this site. I'm making a sequence class for my C++ class. And The thing is in the array that I have, lets say i put in {13,17,38,18}, when i see the current values for the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.