Showing posts with label books. Show all posts
Showing posts with label books. Show all posts

Friday, March 30, 2012

HELP!!-Urgent

"I've created a customer table and I want to give each customer a
unique id e.g 'ES001' But sql server won't let me I've looked at
various books to no avail.

My second question is when I delete a row from a table which had an ID
of 2 when I put in a new row it gives the new row and ID of 3 how do i
make it give the row an ID of 2."You can use a view, trigger or a calculated column to derive a
meaningful surrogate key from your data, or you can generate it in your
application. What exactly do you have a problem with? What do you mean
by "sql server won't let me"?

As for your second question, the whole point of an IDENTITY key (I
assume that's what you are referring to) is that it is internal,
auto-generated and meaningless. If you care about what the value should
be then you shouldn't use IDENTITY. If you are just trying to number
each row then why put that information in the table at all - just
display the numbers when you query it or print a report.
--
David Portas
SQL Server MVP
--|||Salim (s_aboobaker@.hotmail.com) writes:
> "I've created a customer table and I want to give each customer a
> unique id e.g 'ES001' But sql server won't let me I've looked at
> various books to no avail.

CREATE TABLE customers (id char(5) NOT NULLL,
...
CONSTRAINT pk_customers PRIMARY KEY (id))
go
INSERT customers (id, ...)
VALUES ('ES001', ...

> My second question is when I delete a row from a table which had an ID
> of 2 when I put in a new row it gives the new row and ID of 3 how do i
> make it give the row an ID of 2."

INSERT tbl (id, ...)
VALUES (2, ...)

As David said, don't use IDENTITY if you want to specify the key values
yourself.

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

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Ehere to begin? You are doing everything wrong because you do not
understand the basic concepts of RDBMS. A table is not a file; it
models an entity in reality. To find a unique identifier, you need to
look at your data model.

**by definition** the IDENTITY extension in T-SQL can never be a key.
It is a sequential numbering that id derived from the state of the
hardware at the time of insertion. It has nothing whatsoever to do
with your data model or the reality being modelled.

What you have is a file system written in SQL. We used to re-use fixed
length preallocated records in a lot of the early disk OS, just like
you are trying to do.

Please get a book on the basics before you learn all the kludges that
people will give you and learn to do it right.|||--CELKO-- (jcelko212@.earthlink.net) writes:
> Ehere to begin? You are doing everything wrong because you do not
> understand the basic concepts of RDBMS. A table is not a file; it
> models an entity in reality. To find a unique identifier, you need to
> look at your data model.
> **by definition** the IDENTITY extension in T-SQL can never be a key.
> It is a sequential numbering that id derived from the state of the
> hardware at the time of insertion. It has nothing whatsoever to do
> with your data model or the reality being modelled.

Where do you get all the crap from?

This is the original post?

"I've created a customer table and I want to give each customer a
unique id e.g 'ES001' But sql server won't let me I've looked at
various books to no avail.

My second question is when I delete a row from a table which had an ID
of 2 when I put in a new row it gives the new row and ID of 3 how do i
make it give the row an ID of 2."

No talk of IDENTITY, no talk about files. In fact it is obvious that
ES001 has nothing to do with IDENTITY.

> What you have is a file system written in SQL. We used to re-use fixed
> length preallocated records in a lot of the early disk OS, just like
> you are trying to do.
> Please get a book on the basics before you learn all the kludges that
> people will give you and learn to do it right.

All I can say to unexperienced people who are following this group is
to put Joe Celko in your kill files. He can't read the posts he replies
to properly. He repeats misconceptions about SQL Server all over again.
His examples are full of sloppy errors. His tone against unexperienced
users is unacceptable.

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

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||s_aboobaker@.hotmail.com (Salim) wrote in news:d84b91a3.0501280315.1f9a13c4
@.posting.google.com:

> "I've created a customer table and I want to give each customer a
> unique id e.g 'ES001' But sql server won't let me I've looked at
> various books to no avail.
> My second question is when I delete a row from a table which had an ID
> of 2 when I put in a new row it gives the new row and ID of 3 how do i
> make it give the row an ID of 2."

Somebody's not getting their money's worth out of *this* consultant.

Friday, March 23, 2012

HELP! Problem in my code

I am very new to SQL and so far have overcome all my problems with reference books and the internet.

However, I am creating a report which doesn't like something in my code (I'm pretty sure it's connected with the second derived table I've added, as without it the report runs okay, although not with the results I expect to see) and I cannot figure out why.

When I try to preview the report I get this error:

Invalid Object Name 'UNIDATA_PLANNINGL_1'.

So far my code is like this:

SELECT DISTINCT
UNIDATA_PLANNING.R0 AS [Job Record], SUBSTRING(UNIDATA_PLANNINGL.R0, 1, 3) AS [Op No], UNIDATA_PLANNINGL.R1 AS [Op Description],
UNIDATA_PLANNINGL.R43 AS [T/O], UNIDATA_WIP.R0 AS [Job No], UNIDATA_PLANNING.ASSY AS Route, SUBSTRING(UNIDATA_PLANNINGL_1.R0, 1,
3) AS [Assy Op], UNIDATA_PLANNINGL_1.R1 AS [Op Description], UNIDATA_PLANNINGL_1.R43 AS [T/O], derivedtbl_1.OPTRIG1,
UNIDATA_PLANNINGL.R0 AS INVISIBLE2, UNIDATA_PLANNINGL_1.R0 AS INVISIBLE
FROM UNIDATA_PLANNINGL INNER JOIN
UNIDATA_WIPL ON UNIDATA_PLANNINGL.R0 = UNIDATA_WIPL.JOBPLKEY INNER JOIN
UNIDATA_WIP ON UNIDATA_WIPL.R2 = UNIDATA_WIP.R0 INNER JOIN
UNIDATA_PLANNING ON UNIDATA_PLANNINGL.Assy = UNIDATA_PLANNING.R0 INNER JOIN
UNIDATA_PLANNINGL AS UNIDATA_PLANNINGL_1 ON UNIDATA_PLANNING.ASSY = UNIDATA_PLANNINGL_1.Assy AND
SUBSTRING(UNIDATA_PLANNINGL.R0, 1, 3) = SUBSTRING(UNIDATA_PLANNINGL_1.R0, 1, 3) AND
UNIDATA_PLANNINGL.R43 <> UNIDATA_PLANNINGL_1.R43 INNER JOIN
(SELECT R0, SUBSTRING(R0, 1, 3) AS OP1, SUBSTRING(R0, 1, 3) + R43 AS OPTRIG1
FROM UNIDATA_PLANNINGL AS UNIDATA_PLANNINGL_2) AS derivedtbl_1 ON UNIDATA_PLANNINGL.R0 = derivedtbl_1.R0 INNER JOIN
(SELECT UNIDATA_PLANNINGL_1.R0, SUBSTRING(UNIDATA_PLANNINGL_1.R0, 1, 3) AS OP2, SUBSTRING(UNIDATA_PLANNINGL_1.R0, 1, 3)
+ UNIDATA_PLANNINGL_1.R43 AS OPTRIG2
FROM UNIDATA_PLANNINGL_1 AS UNIDATA_PLANNINGL_1_1) AS derivedtbl_2 ON UNIDATA_PLANNINGL_1.R0 = derivedtbl_2.R0
WHERE (UNIDATA_PLANNINGL.R0 LIKE N'%JOB\%') AND (NOT (UNIDATA_PLANNING.ASSY LIKE N'%2-%'))
ORDER BY [Job Record]

Could someone more experienced please explain what I have done wrong and if there is a way to fix it?

Thanks,

CPH

Derived table 2

(SELECT UNIDATA_PLANNINGL_1.R0, SUBSTRING(UNIDATA_PLANNINGL_1.R0, 1, 3) AS OP2, SUBSTRING(UNIDATA_PLANNINGL_1.R0, 1, 3)
+ UNIDATA_PLANNINGL_1.R43 AS OPTRIG2
FROM UNIDATA_PLANNINGL_1 AS UNIDATA_PLANNINGL_1_1) AS derivedtbl_2

You alias the table as UNIDATA_PLANNINGL_1_1 then reference it in the select list as UNIDATA_PLANNINGL_1

In future if you get these warnings, pull out any sub queries and test them independantly. You might also want to use something more meaningfull to alias the table names with but thats your choice

|||

Sam,

Thanks for your quick response. However, the alias UNIDATA_PLANNINGL_1_1 is something VS2005 does. If I amend it to UNIDATA_PLANNING_1 which is the table I have included VS2005 immediately changes it back to UNIDATA_PLANNINGL_1_1.

You're right about the table names, though, but unfortunately that's out of my hands.

Thanks again,

Chris

|||

also in that 2nd derived table - you select from UNIDATA_PLANNINGL_1. Is this a table? you use that name as an alias further up which AFAIK will have no scope inside the sub query.

The query should be:

(SELECT UNIDATA_PLANNINGL_1.R0, SUBSTRING(UNIDATA_PLANNINGL_1.R0, 1, 3) AS OP2, SUBSTRING(UNIDATA_PLANNINGL_1.R0, 1, 3)
+ UNIDATA_PLANNINGL_1.R43 AS OPTRIG2
FROM UNIDATA_PLANNINGL AS UNIDATA_PLANNINGL_1) AS derivedtbl_2

|||

Sam,

Thanks again for your reply and your help. As I have the table UNIDATA_PLANNINGL twice in the query VS2005 has aliased the second occurence of the table as UNIDATA_PLANNINGL_1. I have used multiple occurrences of same tables before without any problem, but this is the first time I've tried adding derived tables.

I have tried your suggestion which has cleared the error.

Your help is much appreciated, thanks again,

Chris