|
We're using Entity Framework 4, ASP.NET MVC 4 and SQL Server 2008 R2. We have some join tables for several entities, for many-many relationships. I'm working to create audit tables/data for each of these relationships/join tables. So far in my research, I've found that triggers are common. We use Windows Authentication, and we have a User table that stores authorized domain users. For our audit tables, we'd like to know which user modified the relationship.
The problem I have is, how do we get the ID from our User table based on the user who updated a relationship using our MVC application? I've looked at some options, and the only thing I can come up with so far, in a trigger, is this:
SELECT @username = nt_username FROM sys.sysprocesses WHERE spid = @@SPID
That will retrieve my username without the domain, which is what I want. I can then compare it to our User table and get the ID where the username matches. But is this the best way to go about it?
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
Matt U. wrote: But is this the best way to go about it? Compared to which other way?
I like triggers; it'll do what it is supposed to do without having to do anything special in the calling code.
The nt_username[^] field from the current process will always be the Windows user name, so that would look correctly too. IIRC then the variable "SYSTEM_USER" should return the same.
There's two or three alternative approaches that come to mind, each of them not as good as triggers.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thanks for the info! I've been working to put it together using triggers and I can't imagine any way that would be simpler. I think I'll just go that route. I only asked because I'm not entirely familiar with triggers, setting up audit tables, and so on.
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
You're welcome
|
|
|
|
|
As much as I loathe triggers spit audit is the one area where they are the right tool for the job.
We have a Modified and ModifiedBy field on EVERY table and use that in the trigger to identify the culprit. We also do not use EF or windows authentication, our apps use app specific credentials to connect to the database.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
hi every body . I learn database I practive coding.plz share me database contact.mdf of microsoft .Thank you!
|
|
|
|
|
What exactly do you want?
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
I need to download and install the
database contacts.mdf, which is used in all samples, but can't find it on
the Web... neither in the manual.
Can anyone help?
|
|
|
|
|
|
i prative example..but i dont have file database Contact.mdf,.,.i cant fint it .plz help me .thank you
http://msdn.microsoft.com/en-us/library/ff847525.aspx
|
|
|
|
|
The article says it's part of the download.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
link download die bro
|
|
|
|
|
Right, which means you're stuck. Try seeing if there is contact information on that page.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
|
|
Can anyone here help in creating a Tables with Primary Key and Foreign Key? I have created tables but failed in Primary Key and Foreign Key. I am very new to Database.
CustInfo DealerInfo AssDealerInfo VehicleInfo LineItem
CustID(PK) DealerID(PK) AsDealerID(PK) VID(PK) LineItemID (PK)
Now I want a Foreign Key dependency on AssDealerInfo that should have access to all the other tables.
How to achieve this?
Your help is much appreciated.
Thanks
Happy Coding!
|
|
|
|
|
Can you please provide more information about attributes you have in your tables and which ones you would like to have Foreign Key constraints on ?
And please explain what have you tried so far. What sort of errors did you get?
If you haven't tried anything yet I would point you to these links:
[General Create table and Primary Keys]
[Foreign Keys]
|
|
|
|
|
Thank you for the article. I have gone through it earlier but this time it is nice to read again. And I am using System i Navigator. Now in general DB2 or AS400 how can I create a Stored Procedure with parameters for Insert Statement?
I totally agree that when everyone in the world is moving with new technologies and migrating data from AS400 to Oracle/SAP or some other things. I am still using this because my boss just loves AS400 like crazy.
Your help is much appreciated.
Thanks
Happy Coding!
|
|
|
|
|
StartDate EndData EmpCode
1/1/2014 1/31/2014 12345
2/1/2014 2/28/2014 12345
3/3/2014 3/25/2014 12345
4/1/2014 4/30/2014 12345
5/1/2014 5/31/2014 12345
7/1/2014 7/31/2014 12345
I need the below output from above table structure using SQL Query.
Output:
StartDate EndData EmpCode
1/1/2014 2/28/2014 12345
3/3/2014 3/25/2014 12345
4/1/2014 5/31/2014 12345
7/1/2014 7/31/2014 12345
|
|
|
|
|
And you want us to decipher the logic of what the output is, oh goody a challenge to start the year off with. It looks like row 1 & 3 are 2 months and the others are 1 month. Why!
Help us to help you, tell us what you are trying to achieve and we might be able to help.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Need to group all the continuous periods. That's why output with 4 rows.
StartDate EndData EmpCode
1/1/2014 1/31/2014 12345
2/1/2014 2/28/2014 12345
need to merger as 1/1/2014 to 2/28/2014.
If no continuous dates, display as it is.
|
|
|
|
|
Ooh that's nasty, I presume you mean consecutive MONTHS not dates! I don't see a simple way to do that in a straight query so I would probably throw it into a cursor and process each record setting a GroupingNo and then query based on the GroupingNo.
Someone with CTE skills should be able to give you a more elegant solution.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
There might be a better way to do this, but the obvious solution would be:
WITH cteStartingPoints As
(
SELECT
A.EmpCode,
A.StartDate,
ROW_NUMBER() OVER (ORDER BY A.StartDate) As RN
FROM
YourTable As A
WHERE
Not Exists
(
SELECT 1
FROM YourTable As B
WHERE B.EmpCode = A.EmpCode
And B.EndDate = DateAdd(day, -1, A.StartDate)
)
),
cteEndingPoints As
(
SELECT
A.EmpCode,
A.EndDate,
ROW_NUMBER() OVER (ORDER BY A.StartDate) As RN
FROM
YourTable As A
WHERE
Not Exists
(
SELECT 1
FROM YourTable As B
WHERE B.EmpCode = A.EmpCode
And B.StartDate = DateAdd(day, 1, A.EndDate)
)
)
SELECT
S.EmpCode,
S.StartDate,
E.EndDate
FROM
cteStartingPoints As S
INNER JOIN cteEndingPoints As E
ON E.EmpCode = S.EmpCode
And E.RN = S.RN
;
http://sqlfiddle.com/#!3/c1331/2[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: but the obvious solution would be Only if you like CTEs, deserves 5
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Thanks, Code works fine. But i have 50 million records in my table. So process is getting slow. Is there any other ways to improve performance?
Thanks in advance...
|
|
|
|