|
Well, if it doesn't compile then the error message would have been useful, but it's never going to be reliable anyway.
Try this - and yes, there may be syntax errors for you to fix
CREATE PROCEDURE dbo.InsertCountry (
@Country NVARCHAR(200) )
AS
BEGIN
begin tran
INSERT INTO Countries
(Country_ID,Country)
select isnull(max(Country_ID),0) + 1, @Country
FROM Countries
commit
END
The reason it would never be reliable is because you could end up with 2 users executing it at the same time and getting the same id. By putting a transaction around it will prevent that.
BTW, your original code error was probably caused by RETURN @LastID , it would skip the rest of the code and exit the proc.
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Here is my delema. A company row can have multiple representations in the security table, based on the priusa or prican values in the company table matching the iid values int he security table, or the values in company table being null, in which case i just want to match the cue of the first iid listed for the key in the security table.
The If section works if I change out the copmany.key references for a literal value, so as far as i can tell its having an issue with the assignment of the value to security.iid = (IF...).
Any ideas?
SELECT company.key, company.name, security.cue
FROM company LEFT OUTER JOIN
security ON company.key= security.key AND security.iid = (IF (select priusa FROM company WHERE key = company.key) is NOT NULL
BEGIN
select priusa FROM company WHERE key= company.key
END
ELSE
IF (select prican FROM company WHERE key= company.key) is NOT NULL
BEGIN
select prican FROM company WHERE key= company.key
END
ELSE
BEGIN
select TOP 1 iid FROM security WHERE key= company.key
ORDER BY security.iid
END)
WHERE (company.name like @name)
|
|
|
|
|
The reason is that you cannot have correlation in inline view when used in from section.
Perhaps something like this would be a simpler approach:
SELECT company.key,
company.name,
(SELECT security.cue
FROM security
WHERE company.key = security.key
AND security.iid = ISNULL(company.priusa,
ISNULL(company.prican,
(select MAX(iid) FROM security WHERE key= company.key))))
FROM company
WHERE (company.name like @name)
Mika
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Thanks much
That works wonderfully for this case. However, what if i wasn't skipping null values. What if the default value was 0 and not null?
|
|
|
|
|
if you want to use more complicated conditions, use COALESCE.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Perhaps the COALESCE function would be better?
|
|
|
|
|
Yep, that would also work out fine.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Thanks.
I'm just getting started and coundn't find anything without knowing the keywords to look for.
|
|
|
|
|
No problem.
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
refer to the title, thanks....
|
|
|
|
|
I would guess that you are referring to the time the database is in active use:
- 24 x 7: 24 hours per day 7 days a week
- 8 x 5: 8 hours per day 5 days in a week (working days and working time)
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
hours you work in a week VS hours you breathe in a week, could be worst!
nelsonpaixao@yahoo.com.br
trying to help & get help
|
|
|
|
|
Hi, im from Germany sorry for my (bad?) english.
Actually i have a really stupid problem.
Ill try to explain it in an abstract example:
1. Imagine you have to log your cd sells, every cd has a different price
2. The price Changes every 6th months and is used for the next 6 month.
3. When i sell the CD, i want to save the date and the current price.
Now my problem:
How do i save the price logging. Do i have to save the current Price in the
CD-Table and save this price for each sell in the selling-table?
Or
Do i have to create a CD/Price/Date table wich contains the prices of a CD of
a period. And i have to look for this price manually.
Or
????
Oh man i hope you understand my problem and know a solution.
Best regards!
|
|
|
|
|
The "proper" way would be to have a price table with an id and a to and from date and log the cd id, price id (and sale date) to a sales table.
eg
Prices Table
ID Price From To
1 14.99 1 jan 2008 30 jun 2008
2 15.99 1 jul 2008 null (indicates current price)
CD Table
CD_ID Title
102 The best of...
103 The Worst of..
Sales Table
CD_ID Price_ID Sale Date
102 1 14 may 2008
102 1 17 Jun 2008
103 1 10 Jan 2008
102 2 14 aug 2008
Hope this helps
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Very similar to my solution below, except you have not stored a reference to the item in the price table. This, i guess, allows you a bit more flexibility in re-using prices across items.
|
|
|
|
|
Great minds...
I would proably (in real life) have a poc coe against the cd and in the price table to allow for different prices depending on the cd, but that wasn't specified
Bob
Ashfield Consultants Ltd
|
|
|
|
|
jesus.online wrote: How do i save the price logging. Do i have to save the current Price in the
CD-Table and save this price for each sell in the selling-table?
Not necessarily, you could represent these relationships with the right data model. I might be tempted by something along the lines of:
TABLE: Item (Holds all CDs to be sold)
ItemId (PK INT)
Title (STRING)
TABLE: Price (holds the price of an item from a date onwards)
PriceId (PK INT)
ItemId (PK INT)
FromDate (DATETIME)
Price (MONEY)
TABLE: Sales (holds a record of every sale, its date and which price it was sold at)
SaleId (PK INT)
ItemId (INT)
PriceId (INT)
SaleDate (DATETIME)
Now you can always tell the current price of an item by looking for the first record in Price where FromDate is less then Current Date (ordered by FromDate DESC)
When storing a sale you store the PriceId along with the ItemId - allowing you to total up sales at each price point if you wish.
edit: You could eliminate ItemId from the Sales table, as you can get to this via the Price table, however a certain amount of de-normalisation is useful for speeding up queries.
|
|
|
|
|
Have to ask
I'm sure you've considered this but why would you eliminate ItemId from Sales instead of PriceId? Wouldn't it make sense to keep the sold item and the sale date if the price was fixed per date range (and you have already itemid in prices)?
Mika
|
|
|
|
|
On reflection, I think you're right and im wrong. However, in practice I wouldnt eliminate either as Id like to get either the price or the item from my sales table.
|
|
|
|
|
Yeah, makes sense that both fk:s are present!
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Thanks a lot J4mieC and Ashfield!
This is exactly what ive been looking for!
Best regards.
|
|
|
|
|
Good Afternoon
I have a Table that get recreated from multiple tables, When its time to recreate it, i found that there are other views that are accssing the table and i cant delete it. in my SP i normaly find out if it exists and drop it like this
if exists (select * from dbo.sysobjects where id = object_id(N'[sde].[PROPERTY_SUMMARY]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [sde].[PROPERTY_SUMMARY]
And the Select into Statement will follow. How do i drop the Table even there are references to this table.
Thank you
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
You have to drop the references first. Why are you dropping it each time - can you not just delete the contents?
|
|
|
|
|
Thank you very much, done as you adviced and everything is cool
Vuyiswa Maseko,
Sorrow is Better than Laughter, it may Sadden your Face, but It sharpens your Understanding
VB.NET/SQL7/2000/2005
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@tshwane.gov.za
|
|
|
|
|
I have got to agree with Paddy
Delete or truncate the table rather than drop it. You should never drop a table as part of a process.
Never underestimate the power of human stupidity
RAH
|
|
|
|