|
I'm not sure if it would work but you could store all of the items in one table and link them to each other, i.e.
ID Code ParentID
-- ---- --------
1 1 NULL
2 1 1 (gives 1.1)
3 1 2 (gives 1.1.1)
3 2 2 (gives 1.1.2)
Using that method you could go to as many or as few levels as required.
|
|
|
|
|
I just found this snapshot from an application.. Any idea what kind of design is adopted??
All generalizations are wrong, including this one!
(\ /)
(O.o)
(><)
|
|
|
|
|
It's impossible to tell how a database is structured just by looking at a screenshot. For all we know, that screen could have taken a long time to pull data in from lots of different tables and munge it around. Having said that, one possible table structure for this would be:
Table: ACC_GROUP
Id Parent Desc
1 NULL Assets
2 1 Current Assets
3 2 Cash on Hand
Table: ACC
Id Group Number Name
1 3 1-1110 Checking Account
2 3 1-1120 Payroll Account
3 2 1-1300 Deposits Paid
From this, you can reproduce that screenshot. Don't forget, you don't have to do everything in one super-complicated SQL query statement. Sometimes it is OK to use cursors, or even (shock horror) to retrieve data and then manipulate it further in code (C#, Java, VB, COBOL, whatever your application happens to use). You won't win any points from the end users for writing clever code. They just want something that works.
But real-world accounting systems are usually more complicated than this so I doubt if that's how it is really done in this particular application. And it's not uncommon to find tables that hold pre-calculated sub-totals at the various levels in order to make displaying total grids like this quick and easy. Ideally you don't duplicate data in a database, but sometimes in the real world it's OK to make that compromise.
|
|
|
|
|
Hi all
am creating table for multiple choice question, each question will have at most 6 choices. so my question is :
is it better to define 6 columns inside that table(one for each column) or just one with special format for it (like using &^& as spertator)?
note: there will be only one answer.
thank you
modified on Tuesday, July 7, 2009 6:57 AM
|
|
|
|
|
I would suggest to have two tables.
Table 1: Question ID and Question Text
Table 2: AnswerID QuestionID and AnswerText
|
|
|
|
|
It's recommended to keep the values in a column atomical; one column/row intersection should hold a single value.
You'd be able to query things a lot easier if you divide it over columns. If things are packed in a single column, chances are that you'd have to resort to parsing the data from that column.
I are troll
|
|
|
|
|
Neither.
The instant you say it's only 6, you'll have a requirement to make it 7.
Use header/detail with QuestionID, then AnswerID/AnswerOption and then you can have 1..N.
Jeremy Likness
http://csharperimage.jeremylikness.com/
|
|
|
|
|
My database holds the complete file path like D:\myfolder\my_file_name.txt & C:\newFile.txt
So, when I sort this, it is sorted according to complete path. Instead, I want to sort it based only on the file name (my_file_name.txt & newFile.txt). Is there any way to write such a query? (If I will implement a sort by myself on file names, it would be slow compared to SQLite sort).
Please suggest.
|
|
|
|
|
I think your design is wrong.
You can store the File Path in 1 column and the corresponding File Names in another column and then perform the sorting on File Names.
In your case, you have to chuck out the File Names from the entire file path and then you have to store it may be in a temporary file and then you have to perform then sorting.
However, the solution is
1) Use a Cursor or While loop and use a split function to split the strings based on '\' character
2) Extract the last string and store it in some temporary table or table variables or you can make your can write a table valued split function.
3) Then you apply the Order by operation.
Hope this helps
Niladri Biswas
|
|
|
|
|
In my earlier post, I have given the idea.
Here is the solution
Say , I have a table tblFileNameSort where I am storing the entire file path
i.e.
FILEPATH
----------
D:\myfolder\my_file_name.txt
C:\newFile.txt
C:\abcFile.txt
E:\bop.txt
I have written a table valued split function
--Created by Niladri Biswas
ALTER FUNCTION [dbo].[fnSplit]
(@oldstring as varchar(100),@delimeter as varchar(1))
RETURNS @mytab table(counter int,stringval varchar(100))
AS
Begin
Declare @newstring as varchar(100)
Declare @pos as int
Declare @i as int
Declare @c as int
set @newstring = '';
set @i = 1
set @c = 0
set @pos = CHARINDEX(@delimeter, @oldstring)
WHILE (@i != 0)
Begin
set @c = @c +1
insert into @mytab(counter,stringval) values(@c,@newstring + Substring(@oldstring,0, @pos))
set @oldstring = Substring(@oldstring,@pos+1,len(@oldstring))
set @pos = CHARINDEX(@delimeter, @oldstring)
set @i = @pos;
if (@i = 0)
Begin
set @i = 0;
set @c = @c +1
insert into @mytab(counter,stringval) values(@c,@newstring + @oldstring)
End
End
return
End
And my stored proc reads like the following
--Created by Niladri Biswas
ALTER PROCEDURE Dbo.SortTxtFile
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- VARIABLE DECLARATION
DECLARE @FILEPATH VARCHAR(50)
DECLARE @FINALSTORAGE TABLE
(
FILENAMES VARCHAR(100)
)
-- STEP 1: DECLARE A CURSOR
DECLARE MYRANDOMCURSOR CURSOR FOR
SELECT FILEPATH
FROM tblFileNameSort
-- STEP 2: OPEN THE CURSOR
OPEN MYRANDOMCURSOR
FETCH MYRANDOMCURSOR INTO @FILEPATH
-- STEP 3: START THE LOGIC
WHILE @@Fetch_Status = 0
BEGIN
-- STEP 4: INSERT THE COMBINED RECORDS INTO TABLE
-- @FINALSTORAGE
INSERT INTO @FINALSTORAGE
SELECT stringval FROM dbo.fnSplit(@FILEPATH,'\')
WHERE stringval LIKE '%.txt'
-- STEP 5: GET THE NEXT RECORD
FETCH MYRANDOMCURSOR INTO @FILEPATH
END
--STEP 6: CLOSE THE CURSOR
CLOSE MYRANDOMCURSOR
--STEP 6: DEALLOCATE THE CURSOR
DEALLOCATE MYRANDOMCURSOR
SELECT * FROM @FINALSTORAGE ORDER BY FILENAMES
END
GO
The output is
FILENAMES
-------------
abcFile.txt
bop.txt
my_file_name.txt
newFile.txt
N.B.~ You must change the database design, as I posted in my earlier reply
Hope this helps.
Niladri Biswas
|
|
|
|
|
Hey Niladri, thanks man for the code. But I am totally illiterate as far as SQL goes. And I am using SQLite so I guess the functions calls etc are different. So, I canot even directly copy your code and use as I dont know where to put what and how to pass parameters etc.. Anyway, I solved my problem by using the Holmes brilliant answer.
many thanks for the help.
|
|
|
|
|
I love being a smartass, try this...
Select *
from Tablename
order by REVERSE(LEFT(REVERSE(path),(CHARINDEX('\',REVERSE(path))-1)))
Not tested but the string massaging works
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I just happened to spot this post, and I have to say, this is pure genius. I'm new to SQL, but with the applications I'm writing, that snippet of code could come in handy for a lot of things. Thanks (even though i'm nothing to do with this post.)
oooo, the Jedi's will feel this one....
|
|
|
|
|
I would love to take credit for it but I probably snaffled it from a tutorial in SQL 6.5, it has been around for a LOOOOnng time.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Brilliant, _insert_synonyms_of_brilliant_here_ man!
My knowledge of SQL is only around 1 hour old out of which 50 minutes was around 2 years back.
I tried ltrim(X,Y) of SQLite but it returned the error that ltrim is not defined although this function is there in docs on SQLite website.. may be some version problems.
Then, I wrote my own function (based on functions in func.c) extractFileName(..) and tried to use "load_extension" to load the c file but could not make it work too.
Finally, I opened SQLite source file "func.c" and added the following two functions and an entry "{ "extractFileName", 1, 0, SQLITE_UTF8, 0, extractFileNameFunc },"
in "sqlite3RegisterBuiltinFunctions"
and in my SQL query, changed " ORDER BY filePath" to " ORDER BY extractFileName(filePath) "
and all worked well!!
Thank you. CP Rocks otherwise where in world one can find ppl like you.
const char* extractFileName(const char* s){
while( *s != '\0' ) ++s;
while( *s != '\\') --s;
++s;
return s;
}
static void extractFileNameFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
const char *z;
assert( argc==1);
if( SQLITE_NULL==sqlite3_value_type(argv[0]) ){
sqlite3_result_null(context);
return;
}
z = sqlite3_value_text(argv[0]);
sqlite3_result_text(context, extractFileName(z), -1, SQLITE_TRANSIENT);
}
|
|
|
|
|
DECLARE @ventyxdbname varchar(50)
SET @ventyxdbname = 'OUTLOG_PACE'
/* */
USE @ventyxdbname;
This gives:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '@ventyxdbname'.
Is it not possible to use a variable in a script for the USE command?
|
|
|
|
|
use nvarchar datatype instead of varchar
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.
|
|
|
|
|
Hi,
Changed it to nvarchar, but still the same message.
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '@ventyxdbname'.
It definitely does not seem to like a variable with USE. Could it be that tis is like the CREATE TABLE command where you cannot use a variable directly?
|
|
|
|
|
declare @DBName varchar(20)
declare @Str varchar(100)
set @DBName = databasename
set @Str = 'use ' + @DBName
exec (@Str)
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.
|
|
|
|
|
Many thanks for trying! I am afraid that does not work either.
declare @DBName varchar(20)
declare @Str varchar(100)
set @DBName = 'OUTLOG_PACE'
set @Str = 'use ' + @DBName
exec (@Str)
select * from dbo.OUT_tbl_Request
--------------
Error message:
Msg 208, Level 16, State 1, Line 8
Invalid object name 'dbo.OUT_tbl_Request'.
The table name is correct, but this is the result if I start the query in master.
Not to worry, there are only 4 places in the script where the database name needs to be changed. I was just trying to be lazy as it is quite a long script and I did not want to miss one. I will use Ctrl+H !!
|
|
|
|
|
What happens if you put a go statement after the exec? For some reason I remember something similar causing me problems and adding a go statement fixed it. Could be completely wrong as I don't have SQL Server in front of me.
|
|
|
|
|
I think I have found the answer - it don't work!!!
From Microsoft
Using EXECUTE with a Character String
In earlier versions of SQL Server, character strings are limited to 8,000 bytes. This requires concatenating large strings for dynamic execution. In SQL Server 2005, the varchar(max) and nvarchar(max) data types can be specified that allow for character strings to be up to 2 gigabytes of data.
Changes in database context last only until the end of the EXECUTE statement. For example, after the EXEC in this following statement is run, the database context is master.
USE master; EXEC ('USE AdventureWorks; SELECT EmployeeID, Title FROM HumanResources.Employee;');
Thanks very much for trying anyway!
|
|
|
|
|
Hi all,
I would need to sync data between two databases with diferent structure. It happens that in one Database (A) i have just 2 or 3 tables (with lot of fields), and on the other database i should have the same data splitted on many other tables. To Sync this i will to create a lot of triggers that are fired on both database tables but some fields data type were also changed like: Where i have field data type NVarchar() on database A the same correspondent field on Database B is Int(). On database A the field can assume the values Yes/No/In Progress but on databse B the same field assumes the values 1/2/3.
Are a lot of triggers to do!
Wich should be the best way to do this? Wich is the best way to convert the data between tables?
Is there some tool to help me doing this like Sybase ETL?
Best Regards,
|
|
|
|
|
You could possibly use SSIS to achieve most of what you're after. How current do both databases need to be?
|
|
|
|
|