473,386 Members | 1,652 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,386 software developers and data experts.

SQL Statemet

47
Can someone tell me what I'm doing wrong here? I'm trying to get this method to return the seqnbr.

Expand|Select|Wrap|Line Numbers
  1.         public override int GetSeqNbr(int EmpId)
  2.         {
  3.             DataSet ds = new DataSet();
  4.             int SeqNbr = 0;
  5.             using (SqlConnection conn = new SqlConnection(SelfServiceConn))
  6.             {
  7.                 StringBuilder sb = new StringBuilder();
  8.                 sb.Append("DECLARE @seq int ");
  9.                 sb.Append("SET @seq = 1 ");
  10.                 sb.AppendFormat("WHILE(SELECT count(*) from {0}.f0115 where WPAN8 = " + @EmpId + " AND WPIDLN = 0 AND WPRCK7 = @seq) > 0",owner.DTA);
  11.                 sb.Append(" BEGIN ");
  12.                 sb.Append("SET @seq = @seq + 1");
  13.                 sb.Append(" END as SeqNbr");
  14.                 SqlDataAdapter da = new SqlDataAdapter(sb.ToString(), conn);
  15.                 da.SelectCommand.Parameters.Add("@EmpId", SqlDbType.Int).Value = EmpId;
  16.                 da.Fill(ds);
  17.                 da.Dispose();
  18.             }
  19.             return SeqNbr;
  20.         }
Mar 2 '09 #1
12 1467
tlhintoq
3,525 Expert 2GB
Why do you beleive you are are doing something wrong? You made no mention of an error message or what you *are* getting that isn't what you expected.

But as a rough guess...
Line 4 you assign zero to SeqNbr
Line 19 you return SeqNbr
So you are probably getting zero returned, right?
Mar 3 '09 #2
kimbred
47
It's not returning the sequence number. Basically, it is supposed to step through the employees records and find the first available sequence number then return it to the calling code. This would allow me to enter new records using available numbers rather than having gaps in the sequence.
Mar 3 '09 #3
tlhintoq
3,525 Expert 2GB
So let's figure it out together. Unless someone here more experienced than I in SQL would like to jump in.

Its is supposed to step through all the employees.
I'm assuming lines 7-12 set up the SQL statement to create the loop going through the employes. It reads like it might behave like a C# 'While' loop.

I think you are setting @seq to 1, checking to see if there is an employee with that number, then incrementing @seq and checking again. If there is no employee with that number, then it should fall through to line 13.

So my question to you is this... at what point do you assign the value of @seq back to SeqNbr? The closest I see is line 13 with END as SeqNbr. Does 'end' somehow hold the value of @seq? Because if it doesn't, I don't see where SeqNbr is ever used except where I earlier mentioned.
Line 4 you assign zero to SeqNbr
Line 19 you return SeqNbr
So you are probably getting zero returned, right?
What value do you get returned right now by SeqNbr?


As a test, if you assign SeqNbr some weird value that you will recognize like

4. int SeqNbr = 1313;

and you get 1313 returned at the end of your run, then you know your query is not affecting SeqNbr in any way. If your query is affecting SeqNbr, then it won't matter what you initialize it to since you are going to change it anyway.
Mar 4 '09 #4
kimbred
47
Each employee has a series of phone records in the database. Each record is assigned a sequence number. Over time, records get changed or deleted so there are gaps in the sequence for a given emplooyee. My goal is to fill that gap. When a new phone record is entered for the employee, I would like to loop through the records to find the first available sequence number and assign it to the record being entered eventually fillling the gap in sequence. The SQL loops through the records and stops when it reaches an available number. I would like it to return that number so it can be used in the insert.
Mar 4 '09 #5
tlhintoq
3,525 Expert 2GB
Ok...

but that doesn't answer the earlier questions I asked you.

Did you try testing with a known identifieable number for your initialization of SeqNbr?
Mar 4 '09 #6
kimbred
47
Yes I did test it and it seems to work fine. I just need to know how to return the value of SeqNbr.
Mar 4 '09 #7
tlhintoq
3,525 Expert 2GB
@kimbred
Then I guess I am confused. You are already returning the value of SeqNbr in line 19 of your original code.
Expand|Select|Wrap|Line Numbers
  1.  return SeqNbr; 
I thought the problem was that SeqNbr wasn't being set to the returned value of your query.
Mar 4 '09 #8
kimbred
47
It always returns 0.
Mar 4 '09 #9
tlhintoq
3,525 Expert 2GB
It always returns zero... Even if you initialize it to some other number?
If you initialize on line 4 as int SeqNbr = 1313; it still returns zero?

That would mean that your query genuinely *is* changing the value. That's a good first step. At least we know that the query is doing *something* and that it *is* affecting the local variable. That's a positive step even if not the final answer.
Mar 4 '09 #10
@kimbred
from the codes u wrote i reaslised that u used a string builder, and also that u initialised ur SeqNbr to zero, but i dont see u passing a value to the SeqNbr. if i got it wrong pls do expaling it more to me.
Mar 6 '09 #11
@kimbred
please check this query in sqlserver
Mar 6 '09 #12
kimbred
47
I've run this in the SQL manager and SEQN = 4. But I can't get it to return that value in my method.

public override int GetSeqNbr(int ABAN8)
{
int retVal = 0;
using (SqlConnection conn = new SqlConnection(SelfServiceConn))
{
StringBuilder sb = new StringBuilder();
sb.Append("DECLARE @seq int");
sb.Append(" SET @seq = 1");
sb.Append(" WHILE(SELECT count(*) from f0115 where WPAN8 = " + @ABAN8 + " AND WPIDLN = 0 AND WPRCK7 = @seq) > 0");
sb.Append(" BEGIN");
sb.Append(" SET @seq = @seq + 1");
sb.Append(" END");
sb.Append(" SELECT @seq AS SEQN");
SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@ABAN8", SqlDbType.Float).Value = ABAN8;
SqlTransaction trans = null;
try
{
conn.Open();
trans = conn.BeginTransaction();
cmd.Transaction = trans;
cmd.ExecuteNonQuery();
trans.Commit();
}
catch (SqlException se)
{
retVal = 0;
trans.Rollback();
}
finally
{
cmd.Connection.Close();
}
PurgeCacheItems(CacheType.SelfService_Cache_);
return retVal;
}
}
}
Mar 25 '09 #13

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

Similar topics

1
by: Stijn Goris | last post by:
hi all, I cannot pass the of(move_uploaded_file($_FILES, $uploaddir . $_FILES)) statemet and need some more clear errorhandling besides the TRUE FALSE story. Can PHP telll me more in detail why...
20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
4
by: Dmitri | last post by:
I just looked at a coworker's stored procedure and this person is dropping 4 Foreign key constraints and then re-adding them after processing the required logic (updating rows in the 4 tables in...
4
by: Elroyskimms | last post by:
Using SQL 2000... tblCustomer: CustomerID int CompanyName varchar(20) HasRetailStores bit HasWholesaleStores bit HasOtherStores bit tblInvoiceMessages:
14
by: Ryan | last post by:
I want to do the following SQL statement in Access. However, it won't allow me to have the secondary part of my join statement and tells me that this is not supported. OK, so Access doesn't support...
5
by: tz | last post by:
The statement below is resulting in this error: C:\Documents and Settings\Teddy\My Documents\Visual Studio Projects\Calendar\Calendar.cs(181): Operator '&&' cannot be applied to operands of type...
4
by: Josh Close | last post by:
Is there a way to remove idle connections? My postgres server is getting serveral hundred idle connections. It's due to a postgres .NET provider not closing the connections properly. I don't want...
3
by: Macroman | last post by:
Totally new to MS Access but kind of finding my way around I tried something simple and it is becomming a pain. I created a simple table as follows Field 1 = "This is Fred" , "This is George"...
2
by: Mamidinx | last post by:
Hi, With the MERGE statement I am using in DB2 it rolls back all the way if an error is encountered. Is there a way to save each of the encountered exception but proceed to the next data record...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.