|
Rob Philpott wrote: Why do you say this isn't really what Sql is designed to do? It seems perfectly acceptable, desirable even (well to me at least!) to have an abstraction where a database can maintain a linked list of data. My operations would be 'return in order', 'delete item in list', 'insert item in list'.
Because SQL is a set based language. It operates more efficiently on small numbers of large set operations rather than large numbers of small set operations.
Traversing a linked list requires a large number of small set operations. e.g. Each SELECT is a set based operation. If you have to traverse a linked list you need to perform a large number of select operations. If you have an Order column in the table then you just need one SELECT statement with an ORDER BY clause.
Remember also, that a linked list is really just an implementation detail of one way to store a set of data. It is useful in certain scenarios. I used linked lists many-a-time when I programmed in C++, but I also used arrays when the need arose.
To get around the insert and delete problem while holding an Order column. the Order column could increment by 2. An insert would put the new row with an odd number (first set based operation), Immediately after, if you are using SQL Server 2005, you could renumber the Order column by using the row numbering feature and multiplying the result by 2 to get the new value of the Order column (second set based operation). Delete operations don't actually have to re-order anything as the Order column is just for ordering and nothing else. Insert operations now trade off some of the speed they once had (because large tables will still take some time) for much faster speed on iteration.
As it stands the WHILE loop suggestion I gave before will have (on anything more than a handful of rows) unacceptable iteration performance when getting the data out. (Large numbers of small set operations)
|
|
|
|
|
Thanks again for you input Colin,
I guess when you reach a situation like this it's probable that the model isn't quite right and needs a rethink. In my experience, everything fits nicely into place when done properly, and this doesn't sit well!
What I was trying to achieve was a generic note facility, where users could add notes to different things. eg. in a database I might have tables Car, Book, Person, Tree representing the objects I'm storing data about. In each of these tables I wanted a 'NoteId' column which could point to the first of a list of notes which could be added and removed at will by users. This way the same note table could be used by different objects.
I suppose the other more traditional way of doing this is via use of one or more intermediate tables which map one-to-many objectIds to Notes eg. CarNotes, BookNotes, PersonNotes, TreeNotes, and I guess this is good because it allows FK constraints to be implemented, but I just wanted to avoid these extra tables. They're almost 'clutter'.
What's the weather like in Scotland? It's been a bit stormy down in London over the past few days.
Regards,
Rob Philpott.
|
|
|
|
|
From what you describe I don't think you need intermediate tables. A compound primary key on the note table might suffice, assuming you don't have one note that needs to be shared among multiple other tables.
The MainId would be what other tables use to link to the notes - so it uniquely defines a group of notes. The SecondaryId is just to uniquely identify each note within the group.
Perhaps that might work. If your system is going to be busy and there may be the scenario where two processes want to create a new group of notes simultaneously then I'd recommend a GUID for the MainId because they won't collide as you cannot use an Identity because it would cause problems when you wanted to put a second note in an existing group and you can't use MAX(MainId) because two processes might try the same thing at the same time.
Your other option is intermediate tables. I don't really think they clutter up the place, but if your think they do, then you could always prefix their name with something to keep them out of the way.
|
|
|
|
|
Rob Philpott wrote: What's the weather like in Scotland? It's been a bit stormy down in London over the past few days.
The rain this afternoon was magnificent. It was almost Mediterranian in its ferocity. It was the same yesterday afternoon just as the sun was setting.
|
|
|
|
|
HI
Guys can any one let me know, which is the largest data type in SQL Server, plz reply ASAP,thankx in advance.
|
|
|
|
|
|
"image" max length 2^31-1 bytes
Note that starting at SQL Server 2005, "image" is deprecated.
Use "varbinary(max)" (max length 2^31-1 bytes) instead.
Mark
|
|
|
|
|
Sir,
I am trying to create a stored procedure in the database in sql server 2005.I have written for the stored procedure .But when i am trying to save it , then the saved stored procedure file doesn't come under the stored procedure hierarchy.and abother problem is when i try to access the stored procedure using asp.net2.0 I mean the saved stored procedure it says the stored procedure i am accessing does not exist.
Please help.
|
|
|
|
|
VS 2003/2005,
when you edit a procdure, "save" means create/alter
SQL Server 2005 Management Studio,
if you want to create a procedure, writing code first then run and create the proc
|
|
|
|
|
I am adding articles to be replicated but I would like to use a cursor to do it due the amount of tables it may take forever to select all from the wizard. I have written a cursor and I am having issues running it. Any suggestion will be greatly appreciated.
Cursor:
declare @tableName varchar(100)
declare c cursor for
select
table_name
from
information_schema.tables
where table_type = 'base table'
open c
fetch next from c into @tableName
while @@fetch_status = 0
begin
--print 'table name: ' + @tableName
--Adding the transactional articles
exec sp_addarticle @publication = N'One_Way_Trans', @article = @tableName , @source_owner = N'dbo', @source_object = @tableName, @destination_table = @tableName, @type = N'logbased', @creation_script = null, @description = null, @pre_creation_cmd = N'drop', @schema_option = 0x000000000000CEF3, @status = 16, @vertical_partition = N'false', @ins_cmd ='CALL sp_MSins_' + @tableName, @del_cmd = N'CALL sp_MSdel_' + @tableName, @upd_cmd = N'MCALL sp_MSupd_' + @tableName, @filter = null, @sync_object = null, @auto_identity_range = N'false'
fetch next from c into @tableName
end
close c
deallocate c
I am getting the following error when I run this script: Line 99: Incorrect syntax near '+'.
Here are the values I can not populate with @tableName.
@ins_cmd ='CALL sp_MSins_' + @tableName, @del_cmd = N'CALL sp_MSdel_' + @tableName, @upd_cmd = N'MCALL sp_MSupd_' + @tableName,
PS: I got the code below to do exactly what I want it to do but notice it is only printing the reults not executing the stored procedure.
declare @tableName varchar(100)
declare c cursor for
select
top 10 table_name
from
information_schema.tables
where table_type = 'base table'
open c
fetch next from c into @tableName
while @@fetch_status = 0
begin
--print 'table name: ' + @tableName
--Adding the transactional articles
print 'exec sp_addarticle @publication = N''One_Way_Trans'', @article = N ''' + @tableName + ''' , @source_owner = N''dbo'', @source_object = N''' + @tableName + ''', @destination_table = N'''+ @tableName + ''', @type = N''logbased'', @creation_script = null, @description = null, @pre_creation_cmd = N''drop'', @schema_option = 0x000000000000CEF3, @status = 16, @vertical_partition = N''false'', @ins_cmd = N''CALL sp_MSins_' + '' + @tableName + ''', @del_cmd = N''CALL sp_MSdel_' + ''+ @tableName + ''', @upd_cmd = N''MCALL sp_MSupd_' + '' + @tableName + ''', @filter = null, @sync_object = null, @auto_identity_range = N''false'''
fetch next from c into @tableName
end
close c
deallocate c
-- modified at 22:23 Friday 29th September, 2006
Greg
Coding makes the world go round!!!
|
|
|
|
|
Never Mind, I got it working. What I did was I ran the "print" script, pulled copied the result and rn it in Quer Analzer. It ran just fine and created all my Articles.
PS: I could not believe I was such a dork. But then again no where tells you that you need to run one script to create another.
Thanks guys.
OBTW: It would be nice if someone can explain wh I can not add a variable name to a stored procedure dnamically.
Greg
Coding makes the world go round!!!
|
|
|
|
|
how can i save the value of a field into a parameter in T-SQL?what i wrote is-
Create Procedure a_c
@C_Name varchar(100),
as
if exists(
select id as 'ID' from ABC where C_Name = @C_Name)
begin
----
end
else
begin
---
end
go
is this syntax correct?and should i declare 'ID'and how can i do it?
|
|
|
|
|
-- create proc
create proc abc
@para int output
as
set @para = 1
go
--exec proc
declare @p int
exec abc @p output
select @p
|
|
|
|
|
I have this info
Desc1................Desc2
-------------......----------
BudgetControl......RamonDilan
DirectorContr......JoseAleman
I wanna to show
Desc1................Desc2.............Desc1.............Desc2
-------------......----------......-------------...---------
BudgetControl......RamonDilan......DirectorContr...JoseAleman
Please help me on that.. Thanks
johnny
|
|
|
|
|
Try something like this:
<<tr>
<asp:repeater>
<td>Desc1 <br/><asp:Label ID="Desc1></td>
<td>Desc2 <br/><asp:Label ID="Desc2></td>
</asp:repeater>
</tr>
Sincerely,
Elina
Life is great!!!
Enjoy every moment of it!
|
|
|
|
|
Hi
Thanks for your response. But I using SQL Statements. I'm using SQL 2000 server in Query Analyser. Do you have any idea?.
Johnny
|
|
|
|
|
There are two options, using T-SQL to dynamically generate the query or use a join and eliminate null records.
If the join will work it is the best option as it is fast. The T-SQL option is usually ugly.
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
|
|
|
|
|
Im returning ratios with rounded off to 3 decimals places, but when the rounding off function doesnt get rid of of the trailing 10 zeroes. How do I get rid of the trailing zeros because it shows in SQL reports. I have tried TRUNCNUM, ROUND, TRUNC. I just cant seem to get rid of those zeroes. Help would much be appreciated.
|
|
|
|
|
Hi!
Try FormatNumber function.... see the example below. If you are not going to define 0 in FormatNumber statement, it will show decimal places.
NewNumber = FormatNumber(OldNumber,0)
|
|
|
|
|
thanks bro, but i sorted it out now. first casted it to a 'real' number then used the 'round' function. and it worked. all trailing zeroes are now removed
|
|
|
|
|
hi all
i have a colunm in my DB ( nvarchar (50)) i use it to store customer's e-mail but this column takes only the first 20 Cahrs from the user's input
i tried to make it 100 in length but nothing work
plz tell me wht to do
thank u
abdelhameed81
|
|
|
|
|
You have a problem in your client-side application. Either the text is being truncated to 20 characters somewhere in that application, or you are using a SqlParameter object (or other similar object appropriate for your database and development language/platform) with size 20.
|
|
|
|
|
What is SQL injection, and how it occurs,
How can we rectify the SQL injection
-
|
|
|
|
|
|
Difference between DELETE, TRUNCATE, DROP
-
|
|
|
|
|