|
Thanks for the reply, got me thinking in the right direction i suppose some sort of TOP specification of 1 while ordering by created date and time would get the row i want.
Do you have any suggesion on how to avoid multiple users trying to add an entry at the same time thou?
What about just having the column as 'unique' then cathing the error and then trying the process again with an incremented reference?
Im not asking if it would be possible, as im sure it is, i just want to know if you can see any holes in the logic that i may have missed.
thanks again
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
1. You can make unique column of reference number and then in exception you insert new reference number.
example:
try<br />
{<br />
}<br />
catch<br />
{<br />
}<br />
finally<br />
{<br />
}
2. You can select last inserted refnumber and then generate new refnumber.
Example:
select top 1 refnumber from mytable order by id desc //ID column must be inc autonumber
string refnum = "091222REF";
string newrefNum = "";<br />
if (DateTime.Now.ToString("yy") != refnum.Substring(0, 2))<br />
{<br />
newrefNum = DateTime.Now.ToString("yy") + "0001REF";<br />
}<br />
else<br />
{<br />
int incNum = int.Parse(refnum.Substring(2, 4)) + 1;<br />
<br />
newrefNum = DateTime.Now.ToString("yy") + incNum.ToString("0000") + "REF";<br />
}
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
|
|
|
|
|
A website I'm writing uses jQuery and a plugin called Flexigrid, that expects an XML document in this format:
<rows>
<page>1</page>
<total>7</total>
<row id="1">
<cell>1</cell>
<cell>SomeName</cell>
<cell>SomeDescription</cell>
</row>
<row id="2">
<cell>2</cell>
<cell>SomeName</cell>
<cell>SomeDescription</cell>
</row>
</rows>
I managed to make it work with the following SQL:
Select
1 As 'page'
,7 As 'total'
,(
Select
ID As '@id'
,(Select ID As 'cell' From Table Where ID = dc.ID For Xml Path(''), Type)
,(Select [Name] As 'cell' From Table Where ID = dc.ID For Xml Path(''), Type)
,(Select [Description] As 'cell' From Table Where ID = dc.ID For Xml Path(''), Type, Elements XsiNil)
From
Table dc
For Xml Path('row'), Type
)
For Xml Path('rows'), Type
Is there a better way to do this? I've scoured Google, and wasn't able to find anything. This works, but it seems like there must be a better way that I'm just not finding.
Thanks.
|
|
|
|
|
hi everybody,
i have two tables student and supervisor
1-Student
studentId studentname
1 john
2 paul
3 Martin
4 tony
.
.
.
14 Mike
2-Supervisor
SupervisorId supervisorName
1 Mark
2 Rodney
3 Wilson
4 Sarah
5 Raj
6 Neil
7 David
i hav 14 students and 7 supervisor i have to allocate each supervisor to 2 students. please could anyone tell me how i can allocate 1 supervisor to 2 sutdents or how can i display output as Allocation Table
like this
Allocation Table
AllocationId Student Supervisor
1 1 1
2 2 1
3 3 2
4 4 2
5 5 3
6 6 3
7 7 4
. . .
. . .
. . .
13 13 7
14 14 7
thanks for any kind help in adv.
regards
learner
|
|
|
|
|
So didn't you understand the answer the first time you asked? If not, try responding to the original post, not starting a new one.
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
hello everybody,
i have two tables Student and Supervisor
StudentId StudentName
1 john
2 paul
3 britney
4 Mike
.
.
.
.
13 beckham
SupervisorId SupervisorName
1 Martin
2 Tony
3 Janet
4 Brad
5 johnson
6 David
7 Raj
how can i allocate 7 supervisor to 13 students equally.
any help would be appricaited.
regards learner
|
|
|
|
|
I guess your table Student should contain column which have any value from Supervisor .
Your Student table have to looks like this :
StudentId StudentName SupervisorId <br />
1 john 1<br />
2 paul 1<br />
3 britney 3<br />
4 Mike 5<br />
.<br />
.<br />
.<br />
.<br />
13 beckham 7
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
|
|
|
|
|
I've got an issue that I'd like to find a better solution to,
I'm inserting a new row into a table, which fires of a trigger that does a set of inserts to replace something that used to be done manually. Previously I had been using @@Identity to retrieve the ID of the new row I inserted, however since I added the trigger I was getting the ID of the last item the trigger inserted.
I've found SELECT IDENT_CURRENT('tbl'), which works, but leaves me concerned with potential concurrency issues.
Any better methods out there or should I consider killing off the trigger to ensure the correct ID will always be returned?
|
|
|
|
|
Read up on Scope_Identity() in BOL. This is a classic error when using triggers.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hello,
I have 2 admin users - say Admin1 and Admin2 - on a XP home edition. I installed my application on a machine using Admin1 login. The application runs smoothly.
Now, i logged off and logged in to the same machine using Admin2 credentials and try to launch the application. But the application could not be launched as the database connection could not be established. The application uses SQL express as the client side database. I have verified that the SQL express service is running on the client.
I tried to reproduce the same case on XP professional (SP2 and SP3 both) by creating 2 admin users. But could not reproduce the issue. The application works wonderfully with both the logins.
Is there anything with SQL Express that is clashing with XP Home???
Do I need to do some special settings on XP Home for SQL Express to work for all admin users ???
Any help would be highly appreciated.
TIA
~Shrikant
|
|
|
|
|
Hi,
Just a little advice needed here. During university I studied databases extensively which included the use of composite primary keys. Now, since leaving university a year or so ago and working in the industry, I haven't come across the use of a composite primary key anywhere. And what's more, I have come across databases where I believe that a composite primary key, using existing data, is the right choice for the table, a single unique field has been added to be used as the primary key.
My question is this, are composite primary keys a good or bad idea? I always thought that they were a good idea but noticing the lack of them in real world applications I beginning to think twice!
|
|
|
|
|
Well, it's debated. The down side of a composite key is when it comes to joins and the like, there more fiddly and take longer. By joining on just a single field your queries are quicker. Some people always have a single field primary key, and the the composite key which you're thinking of as a unique index constraint on the table.
Personally, I don't see anything wrong with a composite primary key. It makes a lot of sense logically and saves a column worths of data.
Perhaps just a question of choice or style.
Regards,
Rob Philpott.
|
|
|
|
|
Ah OK, thanks for the reply.
Having not worked in the industry for too long I haven't really found 'my own style' yet but I do like the idea of using natural data to use as the key rather than adding an extra field, even if it means using a composite key and dealing with it's drawbacks. But then, maybe I'll change my mind once I put it into practice!
Thanks again.
|
|
|
|
|
Theoretically writing, I think composite keys are the way to go because they suggest to the consumer of the database that these are the pieces of data that define a unique row in the table.
Practically writing, if you or programmers that use the database want to use one of the many Object-Relational Mappers out there, quite quickly you'll/they'll start complaining because composite keys make them really hard to use. Quite often you end up with the integral primary key and the unique constraint that Rob Philpot describes above.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
As someone who has been designing data structures for many years there are some things that I avoid, composite primary keys are one of them. Triggers are another, both make the system more difficult to support. However that is my "style" as you noted, you will fall into your own style with experience.
Curtis point about using ORM tolls is the driving decision maker behind my preference. I use a my own custom written ORM and composite keys are a bitch.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Example:
Create table myTable(studentID int,
firstName nvarchar(25),
lastName nvharchar(23)
)
I want the studentID field to be auto generated automatically, like 1,2,3 or 0,1,2 etc each time i insert a record. How do I do that inside a query (not in design mode).
|
|
|
|
|
Hi,
this is the SQL statement PHPMyAdmin comes up with when creating a table with two fields, one of them auto-incrementing:
CREATE TABLE `db1`.`test3` (
`name` VARCHAR( 12 ) NOT NULL ,
`ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY ( `ID` )
) ENGINE = MYISAM
Hope this helps.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
If you mean in SQLServer you must use IDENTITY
IDENTITY is the auto generate mode of an number field
|
|
|
|
|
I need to query a file using DB2's flavor of SQL. My query is selecting rows based on email address and I'm having trouble because the email address contains the @ symbol. Does anyone know of an escape character or something I can use to specify that I'm actually looking for the @ symbol instead of it thinking I'm loading a parmeter or something?
My basic statement is something like this:
SELECT * FROM MyLib.MyFile WHERE email='myemail@hotmail.com'
The statement doesn't error out. It just returns nothing, even when I know that the specified email address is in the file. And it's not an issue with lower/upper case letters, I've looked into that as well.
If anyone has any ideas for me, I'd really appreciate any help.
|
|
|
|
|
Kschuler wrote: any ideas
1. if your table really called 'MyLib.MyFile'?
2. try LIKE 'myemail%hotmail.com' to check there are some matches. It circumvents your @ problem (if that is what it is), but may match more than you anticipate.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Luc Pattyn wrote: 1. if your table really called 'MyLib.MyFile'?
No, my table is not really called that.
Luc Pattyn wrote: 2. try LIKE 'myemail%hotmail.com'
I've thought of this one, but ran into the issue with getting more than I wanted. Please let me know if you think of any other solutions.
|
|
|
|
|
Kschuler wrote: try LIKE 'myemail%hotmail.com'
did you at least try it, to make sure no other problem exists?
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Yes, using
LIKE 'myemail%hotmail.com'
works, but it will return not just records with email of
myemail@hotmail.com
but also returns recordswith emails like:
myemail773@hotmail.com
myemailaddress@hotmail.com
I was hoping to find a way to specify the @ symbol to avoid problems like the one specified above.
|
|
|
|
|
Hi,
OK I suggest you check your database responds to SQL parameters, either parameters by name as in WHERE email=@email or by sequence as in WHERE email=? . So check the documentation on SQL parameters.
If it does not, you could go for "_" instead of "%" since the former should match exactly one character, which is bound to be a "@" in an e-mail address.
However, parameterized queries are the better option, since they protect you against SQL injection attacks, and don't force you to escape special characters such as " ' " and " _ ".
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Unfortunately I cannot use parameters. The whole point of the program that I'm working on is that it lets users type an sql statement to create their own queries to the data. (Program is only used by fellow developers) So I guess I will have to use your suggestion and replace any @ symbols with _ or just let my users know that they need to do that themselves when typing the SQL statement and querying by email address.
Thanks for your input.
|
|
|
|