|
In my own experience, having tables with the same name in two different schemas on the same database has led to issues (when people writing queries have omitted the schema name when referring to the table).
If there is absolutely nothing in common between the databases then I would probably have two separate databases and manage them separately. It rather depends on how things like backups, transaction logs, etc will be handled and if there are benefits to having the schemas held on the same db.
However, if these apps are for the same corporation and use different tables then I would put them on the same database, no need for differentiating schemas and users unless you want to use them to control access, or might want to in the future.
We are currently using schemas in a corporate database to indicate which "area" (application if you like) "owns" the data in the table(s) and using the dbo schema for common data (i.e. no tables with the same name in different schema). I'm yet to discover if this will cause us problems.
As you can probably gather .. "good" or "bad" is quite subjective and depends greatly on what the overall infrastructure is going to look like
|
|
|
|
|
Quote: If there is absolutely nothing in common between the databases then I would probably have two separate databases and manage them separately.
They will have some stuff in common, like the user data (username, email, password ...) and other stuff too, but they will also have some data that will be needed only for them separatly. So I'm goin to use something like you said.
Quote: We are currently using schemas in a corporate database to indicate which "area" (application if you like) "owns" the data in the table(s) and using the dbo schema for common data (i.e. no tables with the same name in different schema).
I'm new to this whole area of DB, so comments/suggestions like this one are really useful (practical not theoretical).
Thanks!
|
|
|
|
|
As an example, let's see what Oracle says about "schema";
A schema is a collection of logical structures of data, or schema objects. A schema is owned by a database user and has the same name as that user. Each user owns a single schema. Schema objects can be created and manipulated with SQL and include the following types of objects: (list of stuff)
Introduction is here[^].
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Hello Friends,
my requirement is, I have table with following fields EmpID,RequestNo,RequestDate,RequestType,RequestStatus.
Now, I need to fetch the top 2 rows of the employee based on the employee ID with request date desc, and should display data of these 2 rows into 1 row with the fields:
I have query to fetch only top 2 rows, now I should know how to display that 2 rows into single rows with columns.
EmpID
Current ReqNo
Current ReqDate
Current ReqType
Current ReqStatus
Previous ReqNo
Previous ReqDate
Previous ReqType
Previous ReqStatus
Kindly help, I googled and found Pivot, but I am not getting exact result.
Thank you so much,
Best Regards,
Priya.
|
|
|
|
|
You claim you are not getting exact result, so show us the code that you have used up to now. Some sample data and your expected results would also be useful
|
|
|
|
|
You could create a temp table with the "current" & "previous" columns and insert data into it from the fetched two top records.
And do it in a simple stored procedure...
|
|
|
|
|
|
For SQL Server 2012 or later:
WITH cte As
(
SELECT
ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As RN,
EmpID,
RequestNo As CurrentReqNo,
RequestDate As CurrentReqDate,
RequestType As CurrentReqType,
RequestStatus As CurrentReqStatus,
LEAD(RequestNo) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqNo,
LEAD(RequestDate) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqDate,
LEAD(RequestType) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqType,
LEAD(RequestStatus) OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As PreviousReqStatus
FROM
YourTable
)
SELECT
EmpID,
CurrentReqNo,
CurrentReqDate,
CurrentReqType,
CurrentReqStatus,
PreviousReqNo,
PreviousReqDate,
PreviousReqType,
PreviousReqStatus
FROM
cte
WHERE
RN = 1
;
LEAD (Transact-SQL) - SQL Server | Microsoft Docs[^]
For SQL Server 2008 or 2008 R2:
WITH cte As
(
SELECT
ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY RequestDate DESC) As RN,
EmpID,
RequestNo,
RequestDate,
RequestType,
RequestStatus
FROM
YourTable
)
SELECT
C.EmpID,
C.RequestNo As CurrentReqNo,
C.RequestDate As CurrentReqDate,
C.RequestType As CurrentReqType,
C.RequestStatus As CurrentReqStatus,
P.RequestNo As PreviousReqNo,
P.RequestDate As PreviousReqDate,
P.RequestType As PreviousReqType,
P.RequestStatus As PreviousReqStatus
FROM
cte As C
LEFT JOIN cte As P
ON P.EmpID = C.EmpID
And P.RN = C.RN + 1
WHERE
C.RN = 1
;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hello Richard,
Thanks lot and lot.
I was looking on this work for a day, it saved my another day. Thanks lot.
Thanks for also sharing the link to learn not only just to copy paste the code shared for me.
Best Regards,
Priya.
|
|
|
|
|
The CTE king strikes again
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
|
You have already posted your question in QA. Reposting the question in other CodeProject forums - even if it's just a link to your question - will only serve to annoy the people you're relying on for help.
Remember, the people who answer questions here are unpaid volunteers, who are spread out across the globe. Be patient - nobody is under any obligation to answer your question within a set time.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I want to calculate the total conditional in the month but remove the records that are not the same month and not the same year, the condition that eliminates the view on the sql access because the result is true or false in the excel file http://www.mediafire.com/file/kq2mr2v6px1uxan/exercise.rar
DOANHTHU2: Sum(IIf(Not IsNull([DONGIA]),[SOLUONG]*([DONGIA]/1000000),[SOLUONG]*([DONGIATT]/1000000)))
DOANHTHU2: Sum(IIf(Not IsNull([DONGIA]) AND (SELECT COUNT(*) AS DEM FROM HDMUABAN WHERE (((HDMUABAN.HD)='M1101') AND ((HDMUABAN.LOAIHD)='FBAN') AND ((Month([NGAYGIAO]))=1) AND ((Year([NGAYGIAO]))=2019)) GROUP BY HDMUABAN.HD, HDMUABAN.NGAYGIAO, HDMUABAN.LOAIHD) AS NUM <=0,[SOLUONG]*([DONGIA]/1000000),[SOLUONG]*([DONGIATT]/1000000)))
TONGDOANHTHU2: IIf(Not IsNull([DOANHTHU2]) And Not IsNull([BSLN]),[DOANHTHU2]+[BSLN],IIf(IsNull([DOANHTHU2]) And Not IsNull([BSLN]),[BSLN],[DOANHTHU2]))
My example only removed one record, I designed the problem of removing many records if the same conditions exist
|
|
|
|
|
So I'm working on a vendor's database trying to search it. It's not normalized real well and the tables are kind of elephanted up as it was originally an MS Access database that they ported to MSSQL. I cannot modify the database but I can create views. In any case, what I currently have is a recursive string builder assembling a SQL statement that searches across multiple tables and views that I created to try to streamline things (one of my first SQL projects, it's not pretty). As you can imagine, it's sloooow. Can anyone point me towards a tutorial that would help me optimize this search?
Current query and code:
string[] strSearch = TextBox1.Text.Split(' ');
string strSelect = "SELECT INVY.ITEMNUM AS Item, " +
"INVY.DESCRIPTION AS Description, STOCK.QTYONHAND AS Quantity, " +
"INVY.UOM AS 'Issue By', STOCK.LOCATION AS Location, INVY.MODEL AS Model, " +
"INVY.UD2 AS IDGroup " +
"FROM INVY LEFT OUTER JOIN STOCK ON INVY.ITEMNUM = STOCK.ITEMNUM " +
"LEFT OUTER JOIN vw_Koogle_ISSREC_Grouping ON INVY.ITEMNUM = vw_Koogle_ISSREC_Grouping.ITEMNUM " +
"LEFT OUTER JOIN INVVEND ON INVY.ITEMNUM = INVVEND.ITEMNUM " +
"WHERE ";
foreach (string strParam in strSearch)
{
strSelect = strSelect + "AND ((INVY.DESCRIPTION LIKE '%" + strParam + "%') OR " +
"(INVY.NOTES LIKE '%" + strParam + "%') OR " +
"(INVY.MODEL LIKE '%" + strParam + "%') OR " +
"(INVY.UD2 LIKE '%" + strParam + "%') OR " +
"(INVY.UD1 LIKE '%" + strParam + "%') OR " +
"(INVY.ITEMNUM LIKE '%" + strParam + "%') OR " +
"(vw_RRD_Koogle_ISSREC_Grouping.EQNUM LIKE '%" + strParam + "%') OR " +
"(vw_RRD_Koogle_ISSREC_Grouping.NUMCHARGEDTO LIKE '%" + strParam + "%') OR " +
"(INVVEND.VENDORITEMNUM LIKE '%" + strParam + "%')) ";
}
strSelect = strSelect.Replace("WHERE AND", "WHERE") +
"GROUP BY INVY.ITEMNUM, INVY.DESCRIPTION, STOCK.QTYONHAND, " +
"INVY.UOM, STOCK.LOCATION, INVY.MODEL, INVY.UD2 " +
"ORDER BY INVY.DESCRIPTION";
sqlMP2Search.SelectCommand = strSelect;
}
modified 1-Mar-19 11:31am.
|
|
|
|
|
Do you want to get so many tutorial URLs that point to nothing, and might not be able to provide a valid result? I am sure, not.
Please share a sample query with us, that you are running and the result you are getting. It would be really helpful to share the schema with us, and we would be able to help you out with the normalization process, or at least share how you can use other services on SQL Server like stored procedures to minimize the overheads of the queries.
You might also need to incorporate caching services to store the results, instead of having to always rerun the query.
Query Optimization and the SQL Server Cache
Please share more details on this, and we will be able to provide you a good solution—or tutorial, as needed.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Updated the question. Hope that helps. Thanks for the feedback!
|
|
|
|
|
With that update in the question, first thing that anybody is going to say is to remove the concatenation and use parameters in the query—it will protect you from SQL Injection.
Secondly, there are so many LIKE operators, why are you using searches on the table, and trying to match every column with the input. A quick tip would be, use a separate search for each column and then try to aggregate the overall results. This would have a little amount of WHERE clause, and the query would end quickly, yours is having multiple OR clauses, which is not letting SQL Server short-circuit the query either and is making it run on each of the records.
I would also not be so sure as to whether any indexing would speed up things, but you can try adding indexes on these columns. Read this for more on that, tsql - SQL Server: Index columns used in like? - Stack Overflow
So, you can start by:
1. Add indexers to the columns as needed. You will know where to add them.
2. Create separate stored procedures to find the query results. Then try to aggregate the result of all queries.
3. Use caching to store the results of the most recent queries.
4. Also, try using full text search capabilities of SQL Server.
Full-Text Search - SQL Server | Microsoft Docs
sql - What is Full Text Search vs LIKE - Stack Overflow
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
It's definitely one of the ugliest things I've ever written but also one of my first. Thanks for the pointers, definitely going to be a big help
|
|
|
|
|
|
|
You're welcome. Should be faster for a search than filtering each table, at the cost of some diskspace.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
HOW CREATE MODULE MANGEMENT OF DOCUMENT IN PHP
|
|
|
|
|
SIMPLE: HIRE A PROGRAMMER AND PAY HIM/HER.
If you have a specific question on PHP, we can help, but we do not provide complete modules.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
First start here[^]
And please don't SHOUT in future - all capitals is considered to be rude on Internet forums don't you know.
|
|
|
|
|
Are there some problems with your keyboard (or just with the Caps-lock)?
|
|
|
|
|