Showing posts with label inserts. Show all posts
Showing posts with label inserts. Show all posts

Friday, March 30, 2012

problem returning rows from SPROC

Heres my problem, the first part selects a row from the database, if
there is no row with the criteria it inserts a row and then returns it,
the problem is the IF statement that inserts the row, never returns the
select after it. if there is a row initially in the database, it
returns the right information, I just can't get it to return the row
after inserting it. Anyone know what the problem could be?

Stored procedure:

ALTER PROCEDURE dbo.CheckCurrentPayPeriod
(@.UserID varchar(50))
AS

BEGIN
-- This SP checks to see if the current PayPeriod exists,
-- if not it will create the payperiod for them and return

DECLARE @.appStartDate DATETIME
DECLARE @.dt DATETIME
DECLARE @.rows int
SET @.appStartDate = (SELECT PayPeriodStart FROM PayPeriodStart)
SET @.dt = GETDATE()

SELECT
UserID
FROM
PayPeriod
WHERE
(PeriodStart <= CONVERT(varchar(10), @.dt, 101)) AND (PeriodEnd >=
CONVERT(varchar(10), @.dt, 101)) AND (UserID = @.UserID)

-- Inserts their new PayPeriod
DECLARE @.PayPeriodID int
if (@.@.ROWCOUNT = 0)
BEGIN
DECLARE @.sDate datetime
DECLARE @.eDate datetime
SET @.sDate = @.appStartDate
SET @.eDate = DATEADD(day, 13, @.sDate)

INSERT INTO
PayPeriod
(UserID, PeriodStart, PeriodEnd)
VALUES
(@.UserID, @.sDate, @.eDate)

/*EXEC @.PayPeriodID = InsertPayPeriod @.UserID, @.sDate, @.eDate*/

SET @.PayPeriodID = @.@.IDENTITY

SELECT * FROM PayPeriod WHERE PayPeriodID = @.PayPeriodID

RETURN

END
else
RETURN
END(dkode8@.gmail.com) writes:
> Heres my problem, the first part selects a row from the database, if
> there is no row with the criteria it inserts a row and then returns it,
> the problem is the IF statement that inserts the row, never returns the
> select after it. if there is a row initially in the database, it
> returns the right information, I just can't get it to return the row
> after inserting it. Anyone know what the problem could be?

So how do you run the procedure? If you run it from Query Analyzer,
you will see something like:

UserID
--

(0 rows affected)

(1 row affected)

PayPeriodID UserID PeriodStart PeriodEnd
---- -- ---- ---
1 <value> <value> <value
(1 row affected)

If you run this from a client program, you must be able to handle these
three result sets. This means that if you use ADO - to take an example -
you should have to use .NextRecordset to navigate through the record sets.
Note here that the second record set is closed - that record sets consists
of the rowcount only.

However, it's probably better to rewrite the procedure:

CREATE PROCEDURE dbo.CheckCurrentPayPeriod (@.UserID varchar(50)) AS
BEGIN
-- This SP checks to see if the current PayPeriod exists,
-- if not it will create the payperiod for them and return
DECLARE @.appStartDate DATETIME
DECLARE @.dt DATETIME
DECLARE @.rowc int
DECLARE @.PayPeriodID int

SET NOCOUNT ON

SET @.appStartDate = (SELECT PayPeriodStart FROM PayPeriodStart)
SET @.dt = GETDATE()

SELECT @.PayPeriodID = PayPeriodID
FROM PayPeriod
WHERE PeriodStart <= CONVERT(char(8), @.dt, 112)
AND PeriodEnd >= CONVERT(char(8), @.dt, 112)
AND UserID = @.UserID
SELECT @.rowc = @.@.rowcount

-- Inserts their new PayPeriod
IF @.rowc = 0
BEGIN
INSERT INTO PayPeriod (UserID, PeriodStart, PeriodEnd)
VALUES (@.UserID, @.appStartDate, DATEADD(day, 13, @.sDate))
SET @.PayPeriodID = @.@.IDENTITY
END

SELECT PayPeriodId, UserId, PeriodStart, PeriodEnd
FROM PayPeriod
WHERE PayPeriodID = @.PayPeriodID
END

Observations:

o SET NOCOUNT ON removes the closed recordset for the rowcount from
the INSERT statement.

o Use style 112 when chopping of time from datetime values. 112 gives
you the format YYYYMMDD, which is always interpreted the same. Format
could be reinterpreted if the user has an unexpected language setting.

o Since @.@.rowcount is volatile - update after each statement, I catch
into a local variable immeidately, and glue that SELECT directly to
the SELECT I'm catching rowcount for.

o SELECT * in production is not good practice. Always explicitly list
which columns you want returned.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.aspsql

Monday, February 20, 2012

Problem inserting integers and date in a sql server 2005 datatable row and selecting it af

Hi,

I have soma ado.net code that inserts 7 parameters in a database ( a date, 6 integers).

I also use a self incrementing ID but the date is set as primary key because for each series of 6 numbers of a certain date there may only be 1 entry. Moreover only 1 entry of 6 integers is possible for 2 days of the week, (tue and fr).

I manage to insert a row of data in the database, where the date is set as smalldatetime and displays as follows: 1/05/2007 0:00:00 in the table.

I want to retrieve the series of numbers for a certain date that has been entered (without taking in account the hours and seconds).

A where clause seems to be needed but I don't know the syntax or don't find the right function

I use the following code to insert the row :

command.Parameters.Add(new SqlParameter("@.Date", SqlDbType.DateTime, 40, "LDate"));

command.Parameters[6].Value = DateTime.Today.ToString();

command.ExecuteNonQuery();

and the following code to get the row back (to put in arraylist):

"SELECT C1, C2, C3, C4, C5, C6 FROM Series WHERE (LDate = Today())"

WHERE LDate = '" + DateTime.Today.ToString() + "'"

Which is the correct syntax? Is there a better way to insert and select based on the date?

I don't get any error messages and the code executes fine but I only get an empty datatable in my dataset (the table isn't looped for rows I noticed while debugging).

Today's date is in the database but isn't found by my tsql code I think.

Any help would be greatly appreciated!

Grtz

Pascal

Instead of

command.Parameters.Add(new SqlParameter("@.Date", SqlDbType.DateTime, 40, "LDate"));

command.Parameters[6].Value = DateTime.Today.ToString();

command.ExecuteNonQuery();
use

command.Parameters.Add(new SqlParameter("@.Date", SqlDbType.DateTime));


However you could use GetDate() (server local time) or GetUtcDate() (server time in GMT)
The select could be

"SELECT C1, C2, C3, C4, C5, C6 FROM Series WHERE LDate DATEADD(day, -1, GetDate()) ANDBETWEEN GETDATE()"

will give you a 24hr windows of records.

|||

thx Tatworth for your quick reply

Tested it quickly but getting this sqlexception: "An expression of non-boolean type specified in a context where a condition is expected, near 'DATEADD' "

Don't have the time to investigate what the select code exactly does and how it works but will do tomorrow and keep you informed!

|||Should have been

"SELECT C1, C2, C3, C4, C5, C6 FROM Series WHERE LDateBETWEEN DATEADD(day, -1, GetDate()) ANDGETDATE()"

The between go in the wrong place!

|||

YES! It worked... THANKS

BUT: do not understand why this needs to be put like this and we can't simply use the GetDate() function

When I follow the logic of the functions and comparison one would think it will return the day of yesterday?

|||

>BUT: do not understand why this needs to be put like this and we can't simply use the GetDate() function

Let me run getDate on my Server
SELECT GetDate(), DATEADD(day, -1, GETDATE())

2007-05-05 09:59:11.280 2007-05-04 09:59:11.280

It returns not just the date but the time as well, thus the BETWEEN will select all records with date/time from 09:59 yesterday to 09:59 today.