|
sindhuan wrote: Is this the right way to do??
No, because
dc.users.InsertOnSubmit("us");
won't even compile, because you're trying to insert a string "ur". Your code is expecting an entity of type user. So you want
dc.users.InsertOnSubmit(us);
So, you first want to query the user to get the name, then insert the new row.
using (MyDataContext dc = new MyDataContext())
{
var userName = (from u in dc.tblUsers
where u.Id = someId
select u.UserName).FirstOrDefault();
user us = new user
{
Date = DateTime.Now,
name = userName
};
try
{
dc.users.InsertOnSubmit(us);
}
catch(Exception e)
{
}
}
You will have to adjust the data context and table names and column names, but this should get you started.
If it's not broken, fix it until it is
modified 14-May-12 18:00pm.
|
|
|
|
|
Hi all,
Trying to discover an appropiate way to perform the audit of a database, I have discovered a piece of code that seems to be quite appropiate. This is there:
http://www.simple-talk.com/sql/database-administration/pop-rivetts-sql-server-faq-no.5-pop-on-the-audit-trail/[^]
What matters right now is the following lines of code, which are difficult for me to be understood properly:
-- Get primary key columns for full outer join
SELECT @PKCols = COALESCE(@PKCols + ' and', ' on') + ' i.' + c.COLUMN_NAME + ' = d.' + c.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk, INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
WHERE pk.TABLE_NAME = @TableName
AND CONSTRAINT_TYPE = 'PRIMARY KEY'
AND c.TABLE_NAME = pk.TABLE_NAME
AND c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME
This piece of code is placed inside a trigger writen on Transact-SQL (SQLServer) code. I understand more or less what is the programmer trying to get by using these lines, but what I don't understant (probably because I'm quite new on SQLServer) is what are the 'i.' and 'd.' references. By accessing the link you can discover the full code, but I had not been able to discover anythink on the code preceding it.
Another part of the code above that is a bit confusing is the following:
@PKCols = COALESCE(@PKCols + ' and', ' on')
COALESCE returns the first non-null expression, but it's difficult for me to understand the meaning of the sentence inside the complete expression shown above.
Thanks in advance, any kind of help (links, direct answer...) will be wellcome.
modified 14-May-12 4:25am.
|
|
|
|
|
You need to take the i and d in context of the rest of the query, they probably represent (alias) the inserted and deleted or possibly data records based on the query.
The coalesce in this case services the composite (2+ fields) primary key requirement.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
The code preceding the lines I pasted above are the following:
CREATE TRIGGER tr_trigtest ON trigtest FOR INSERT, UPDATE, DELETE
AS
DECLARE @bit INT ,
@field INT ,
@maxfield INT ,
@char INT ,
@fieldname VARCHAR(128) ,
@TableName VARCHAR(128) ,
@PKCols VARCHAR(1000) ,
@sql VARCHAR(2000),
@UpdateDate VARCHAR(21) ,
@UserName VARCHAR(128) ,
@Type CHAR(1) ,
@PKSelect VARCHAR(1000)
--You will need to change @TableName to match the table to be audited
SELECT @TableName = 'trigtest'
-- date and user
SELECT @UserName = SYSTEM_USER ,
@UpdateDate = CONVERT(VARCHAR(8), GETDATE(), 112)
+ ' ' + CONVERT(VARCHAR(12), GETDATE(), 114)
-- Action
IF EXISTS (SELECT * FROM inserted)
IF EXISTS (SELECT * FROM deleted)
SELECT @Type = 'U'
ELSE
SELECT @Type = 'I'
ELSE
SELECT @Type = 'D'
-- get list of columns
SELECT * INTO #ins FROM inserted
SELECT * INTO #del FROM deleted
-- Get primary key columns for full outer join
SELECT @PKCols = COALESCE(@PKCols + ' and', ' on')
+ ' i.' + c.COLUMN_NAME + ' = d.' + c.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
WHERE pk.TABLE_NAME = @TableName
AND CONSTRAINT_TYPE = 'PRIMARY KEY'
AND c.TABLE_NAME = pk.TABLE_NAME
AND c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME
So, there are no mentions about 'd.' or 'i.'. Are "d" and "i" alias for these tables by default?
|
|
|
|
|
One thing to remember about T-SQL is that by default if you have a null in an expression the expression is null. Thus
COALESCE(@PKCols + ' and', ' on') would return ' on' if @PKCols is null as NULL + ' and' returns NULL.
Also NULL cannot be compared to another NULL as in WHERE NULL = NULL because this returns NULL not true or false. This gets me on occasion. Besides the COALESCE function there is ISNULL, used like
WHERE ISNULL(@PKCols, '') <> ''
Hope this is more helpful than confusing.
|
|
|
|
|
Hi all,
How do you use the REGEXP_SUBSTR function to split a string into two?
Specifically, I want to split a string into two where " and " is the delimiter (note the whitespace).
So for example, if the string is "xande and zem and dem and doe", I want string 1 to be "xande" and string 2 to be "zem and dem and doe".
Any help would be greatly appreciated.
Thanks
|
|
|
|
|
case
when instr(mystring, ' and ') > 0 then substr(mystring, 0, instr(mystring, ' and ')-1)
else mystring
end as string1
case
when instr(mystring, ' and ') > 0 then substr(mystring, instr(mystring, ' and ') + length(' and '))
else null
end as string2
|
|
|
|
|
Worked perfectly. Thanks Jorgen!
|
|
|
|
|
|
hi
i could not understand the "timestamp"
what is timestamp and how can i get month from timestamp
what is difference between timestamp and datatime in sql
thanks in advance
vijay kumar
|
|
|
|
|
Is a data type that exposes automatically generated, unique binary numbers within a database. timestamp is generally used as a mechanism for version-stamping table rows. The storage size is 8 bytes. The timestamp data type is just an incrementing number and does not preserve a date or a time. To record a date or time, use a datetime data type.
You're welcome
Bastard Programmer from Hell
|
|
|
|
|
Hello Everyone
I have created a WPF project (C# language) and I also have a MS Access 2007 database.
I'm trying to display the database table name in to datagrid, but i can't figur out the SQL query how to do it. The code below that I wrote it's not correct I think maybe someone can help me please and solve it...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string CmdString = string.Empty;
using (OleDbConnection myConnection = new OleDbConnection(ConString))
{
CmdString = "SELECT FoodMenu FROM MSysObjects WHERE (FoodMenu Not Like 'MSys*') AND (Type In (1,4,6)) ORDER BY FoodMenu";
OleDbCommand comm = new OleDbCommand(CmdString, myConnection);
OleDbDataAdapter sda = new OleDbDataAdapter(comm);
myConnection.Open();
DataTable dt = myConnection.GetSchema("Tables");
foreach (DataRow dataRow in dt.Rows)
{
DataGridMenuTables.Items.Add(dataRow["FoodMenu"].ToString().Trim());
}
}
}
Kind regards
Roni
|
|
|
|
|
LAPEC wrote: DataTable dt = myConnection.GetSchema("Tables");
That should work.
LAPEC wrote:
Do you mean DataSource?
LAPEC wrote: dataRow["FoodMenu"].ToString()
It's already a string, don't use ToString, just cast it ((string) dataRow["FoodMenu"]).Trim()
|
|
|
|
|
The simplest way is to create a Stored Procedure and put below mentioned SQL Query to fetch the list of tables in database.. then Bind those records to a DataGrid in C#.
SQL Query is: SELECT * FROM information_schema.tables
- Happy Coding -
Vishal Vashishta
|
|
|
|
|
I think he's using Microsoft Access, not Sql Server
Bastard Programmer from Hell
|
|
|
|
|
What is the best,simple and quick database for writing a dictionary like Babylon?
What database system Babylon use?
|
|
|
|
|
davood_b wrote: What database system Babylon use?
I don't even know what that is.
davood_b wrote: for writing a dictionary like Babylon?
Ditto.
davood_b wrote: What is the best,simple and quick database
SQL Server Express.
|
|
|
|
|
..and install a server-class database-application, for a simple local data lookup?
Bastard Programmer from Hell
|
|
|
|
|
Eddy Vluggen wrote: a simple local data lookup
I have no idea that that's the case.
|
|
|
|
|
davood_b wrote: What is the best
There ain't no such thing.
davood_b wrote: simple
Any SQL92-compliant database.
davood_b wrote: database for writing a dictionary like Babylon?
A local file-based database, like SqlCe, Sqlite or MSAccess.
davood_b wrote: What database system Babylon use?
Dunno, ask them. Ergane is using MSAccess, and you can download those dictionaries for free. Translates using Esperanto as an intermediate language (English -> Dutch would be English -> Esperanto -> Dutch). I don't have a download-link, you'll have to Google yourself.
Bastard Programmer from Hell
|
|
|
|
|
Hi, Just would like to ask for help for the xquery that I need to use for the xml string below to populate my table with the following dimension. This means that for each manager, I can have more than 1 row (depending on the number of staff) as I have flattened the table to eliminate joins on multiple table vars. Sorry, I just couldn't get my xquery right.
Finally, if I have my xsd, how do I validate an xml input such as above with sql?
Many thanks.
Manager Name Type Staff
declare @myOrg xml
set @myOrg = '="1.0"="utf-8"
<Department>
<Subdivision>
<Managers>
<Manager>
<Name>Martin</Name>
<Type>Full Time</Type>
<Purpose>Subdivision 1 Shadow</Purpose>
<Description>Project Management, Operations</Description>
<FullName>Martin Fuller</FullName>
<StaffList>
<Staff>H1 Level 4, 345Y</Staff>
<Staff>H1 Level 4, DS23</Staff>
</StaffList>
</Manager>
<Manager>
<Name></Name>
<Type></Type>
<Purpose></Purpose>
<Description></Description>
<FullName></FullName>
<StaffList>
<Staff></Staff>
</StaffList>
</Manager>
</Managers>
</Subdivision>
</Department>'
----------------------------------------------------------
Lorem ipsum dolor sit amet.
|
|
|
|
|
Hi,
How can i read SQL Database 2008 LDF (Log) file programmatically (<code><big>with T-SQL OR C#</big> ).
Its very Important for me.
Plz Help
|
|
|
|
|
The format isn't publicly available. Why do you need them?
Bastard Programmer from Hell
|
|
|
|
|
I wanna to check the action placed on the database at different times and dates.(when insert occured in tblName or what palce for data in Update Statement OR ...)
|
|
|
|
|
If you want to read the log file, you'll have to contact Microsoft for a license.
What you want could be easily achieved using a trace, logging it's results. To a textfile or some other database. Google for "Sql Trace" and you should find some interesting results, even from CodeProject
Bastard Programmer from Hell
|
|
|
|