Showing posts with label columns. Show all posts
Showing posts with label columns. Show all posts

Thursday, March 29, 2012

Auto fill in colums trough foreign key relationship

Hi,
I have a table users where there is a user_id and an department column.
Also i have a table called KRS where there are the same columns, when a userid is given i want to auto fill in the departmentid,
Can someone help me with this?
Cheers WimYou want help creating a denormalized database?

[sigh...]

If you absolutely need to do this, you can accomplish it through an INSERT trigger on your KRS table.

Why are you duplicating data like this?|||You want help creating a denormalized database?

[sigh...]

If you absolutely need to do this, you can accomplish it through an INSERT trigger on your KRS table.

Why are you duplicating data like this?I keep trying to shoot myself it the foot, but the rifle wobbles when I pull the trigger. Can one of you guys hold it for me?

I actually thought about trying to formulate a reply to this question, but nothing I wrote seemed fit to post. I'm thinking that a VIEW would make life a lot easier in the long run, what do you think?

-PatP

Thursday, March 22, 2012

Authentication not working!

Hey there,

I have a table [user] in a SQLExpress database [Database.mdf] in the App_Data folder. There are two columns. [userId] and [password]. I want to authenticate the user who wants to log in through a Login control. the user should have his userId and password in the [user] table. I digged and made my code. but it doesn't work it keeps giving me that the user is not authenticated. i guess it has no errors. please tell me if you find the error. here is the code:

1protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)2 {3 SqlConnection conExpress =new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Database;Integrated Security=True;User Instance=False");//Create the server connection45try6 {7string id_user = Login1.UserName.Trim();//Get the username from the control8string pass_user = Login1.Password;//get the Password from the control910 //These are a sql and connection Examples11string sql ="SELECT userId, password FROM user WHERE userId = @.param_Id AND password = @.param_Password";12 SqlCommand comSQL =new SqlCommand(sql, conExpress);//Create the sql command using sql string and sql connection1314 //Add the sql parameters15 comSQL.Parameters.AddWithValue("@.param_Id", id_user);//New on VS 2.016 comSQL.Parameters.AddWithValue("@.param_Password", pass_user);1718string cod_user ="";19string name_user ="";2021//Open database connection22 conExpress.Open();23 SqlDataReader dr = comSQL.ExecuteReader();24while (dr.Read())25 {26 cod_user = dr.GetValue(0).ToString();//The coduser is unique onto database table27 name_user = dr.GetValue(1).ToString();28 }29 conExpress.Close();//Close Database Connection30if (cod_user !="")//The user exist onto database31 {32//Create the session vars33 Session["coduser"] = cod_user;34 Session["nameuser"] = name_user;35 e.Authenticated =true;//Grant the access, Goes to DestinationPageUrl36 }37 }38catch(Exception)//On Login Error39 {40 e.Authenticated =false;//Confirm that you are out41 conExpress.Close();//On any error case, close the database connection42 }43 }

The way I've used the Login control with custom authentication (without MembershipProvider) is

processing the event of the Login Control:

protected

void Login1_LoggingIn(object sender,LoginCancelEventArgs e)

and if the credentials is valid i create the autheentication cookie:

FormsAuthentication.SetAuthCookie(userName,false);

and Redirect to the page user has requested.

If the credentials are wrong I set :

e.Cancel =

true;

and

Login1.FailureText =

"Unable to validate credentials";

I'm not sure this is the right way of doing that, but it works for me.

Hope this will help you.

Cheers,

Yani

sql

Thursday, March 8, 2012

auditing

I have another question.
I want to create audit table with columns (userID, date, tableAffected,
ColumnAffected).
This table should have data from tables that I want to trace. It easy to
collect data about time and users, but I don't know how to collect data abou
t
which table and which column is affected.
So, if i have table employees employeeID,Lastname,Firstname I wolud like to
have 3 record in my Audit table:
uros; 11/11/2005; employees; EmployeeID
uros; 11/11/2005; employees; Lastname
uros; 11/11/2005; employees; Firstname
Do you have any suggestions?Hi,
I wat to give my suggestion here , there might be better suggestions pouring
for this prob. so keep checking,
Create trigger for all ur tables and in triggers use Updated(col_name) to
check whether column got updated or not.
--
Vishal Khajuria
9886170165
IBM Bangalore
"uros" wrote:

> I have another question.
> I want to create audit table with columns (userID, date, tableAffected,
> ColumnAffected).
> This table should have data from tables that I want to trace. It easy to
> collect data about time and users, but I don't know how to collect data ab
out
> which table and which column is affected.
> So, if i have table employees employeeID,Lastname,Firstname I wolud like t
o
> have 3 record in my Audit table:
> uros; 11/11/2005; employees; EmployeeID
> uros; 11/11/2005; employees; Lastname
> uros; 11/11/2005; employees; Firstname
> Do you have any suggestions?|||hi Uros,
You can design a SQL server trace using sql profiler
and save the trace to a file.
for optimum performance. you can run a trace
on another machine which is the standard way of profiling sql server.
in this way your auditing doesn't disturb
the production server
thanks,
Jose de Jesus Jr. Mcp,Mcdba
Data Architect
Sykes Asia (Manila philippines)
MCP #2324787
"uros" wrote:

> I have another question.
> I want to create audit table with columns (userID, date, tableAffected,
> ColumnAffected).
> This table should have data from tables that I want to trace. It easy to
> collect data about time and users, but I don't know how to collect data ab
out
> which table and which column is affected.
> So, if i have table employees employeeID,Lastname,Firstname I wolud like t
o
> have 3 record in my Audit table:
> uros; 11/11/2005; employees; EmployeeID
> uros; 11/11/2005; employees; Lastname
> uros; 11/11/2005; employees; Firstname
> Do you have any suggestions?|||Uros,
I have this design working except I have OldValues and NewValues columns
instead of ColumnAffected. These two columns are for recording affected
columns names and values in an xml style. This allows for having just one
row per action. In addition I have EventType to tell update from insert from
delete and RowID column to link back to the original table record. The work
of writing to the audit table is done in a universal trigger that can be
dropped into a table design with no modifications. In that trigger the
following is user to get the table name:
select object_name(parent_obj)
from sysobjects
where id = @.@.procid
and columns_updated() function output is parsed to get to the columns
affected.
Ilya
"uros" <uros@.discussions.microsoft.com> wrote in message
news:0D4071F5-7445-43E1-B904-B4F43DB020FF@.microsoft.com...
> I have another question.
> I want to create audit table with columns (userID, date, tableAffected,
> ColumnAffected).
> This table should have data from tables that I want to trace. It easy to
> collect data about time and users, but I don't know how to collect data
about
> which table and which column is affected.
> So, if i have table employees employeeID,Lastname,Firstname I wolud like
to
> have 3 record in my Audit table:
> uros; 11/11/2005; employees; EmployeeID
> uros; 11/11/2005; employees; Lastname
> uros; 11/11/2005; employees; Firstname
> Do you have any suggestions?

audit tables with composite keys

I am trying to write triggers on each tables in my database to audit data changes. My AuditLog table consists of the following columns -

LoginName varchar(100) - user name

Action varchar(5) - this will store 'INSERT','UPDATE','DELETE'

TableName varchar(30) - name of the table updated

PrimaryKey int - primary key of the record updated

ColumnName varchar(30) - name of the column updated

OldValue varchar(1000) - old value converted to varchar

NewValue varchar(1000) - new value converted to varchar

RecUpdDate datetime - record update date.

This table design will work for tables with single column primary keys. However, it will not work for tables with composite primary keys. Any suggestions on how to make this work with composite primary keys? I prefer not to change the tables in my database to use single column primary key.

Thanks in advance.

I had same kind of audit table. But i had PrimaryKey column as varchar. and we concatinate primarykeys of the table and insert in this table.

Madhu

|||

I had same kind of audit table. But i had PrimaryKey column as varchar. and we concatinate primarykeys of the table with a delimitter and insert in this table.

Madhu

|||

If you concatinate the primary keys, you need to make sure it's done is specific column order. If you want to use the primary key to locate the record in the data table, then you need to parse the string first. Do you find any performance issues with this method?

audit tables with composite keys

I am trying to write triggers on each tables in my database to audit data changes. My AuditLog table consists of the following columns -

LoginName varchar(100) - user name

Action varchar(5) - this will store 'INSERT','UPDATE','DELETE'

TableName varchar(30) - name of the table updated

PrimaryKey int - primary key of the record updated

ColumnName varchar(30) - name of the column updated

OldValue varchar(1000) - old value converted to varchar

NewValue varchar(1000) - new value converted to varchar

RecUpdDate datetime - record update date.

This table design will work for tables with single column primary keys. However, it will not work for tables with composite primary keys. Any suggestions on how to make this work with composite primary keys? I prefer not to change the tables in my database to use single column primary key.

Thanks in advance.

I had same kind of audit table. But i had PrimaryKey column as varchar. and we concatinate primarykeys of the table and insert in this table.

Madhu

|||

I had same kind of audit table. But i had PrimaryKey column as varchar. and we concatinate primarykeys of the table with a delimitter and insert in this table.

Madhu

|||

If you concatinate the primary keys, you need to make sure it's done is specific column order. If you want to use the primary key to locate the record in the data table, then you need to parse the string first. Do you find any performance issues with this method?

Friday, February 24, 2012

Attempting to transpose/pivot columns to rows... I think.

I have something of a challenge for you folks (at least it's very challenging for me).

I have a table that has data that looks like this:

DateOrderNum_WorkDay

2006-06-1__AA1

2006-06-1__AB1

2006-06-2__BA2

2006-06-2__BB2

2006-06-2__BC2

2006-06-5__CA3

2006-06-5__CB3

etc.

So, there are dates that orders happened and each date is marked as the 1, 2, 3, etc. business day of the month.

What I am trying to write is a SQL statement that will display results thus:

Day1Day2Day3_etc.

AA__BA_CAetc.

AB__BB_CBetc.

Is this making any sense to anyone? Basically, I need to turn the WorkDay rows into columns and display all the info for each WorkDay under them.

I have a feeling this isn't hard, but I am fairly new to writing SQL so any advice would be pure gold to me. Thanks!

2 questions.

1 - What version of SQL Server are you using? Is it 2005?

2- Is the set of Work days dynamic, or do you know the specific work days when you are writing the query. More specifically, do the columns in the result need to be generated dynamically from the data in the table?

|||

Ryan,

Thanks for the quick reply! Here are the answers to your questions:

1. I'm using SQL Server 2005 (and I just found the PIVOT command which is new in 2005 apparently).

2. The set of workdays is dynamic. Depending on the size of the month and holidays there could be as few as 20 or as many as 23. And if I understand your second question then yes the columns will need to be generated dynammically. The data in the table will be changing daily and this SQL statement is generating a grid view on a web page.

Thanks again!

|||

Unfortunately, the dynamic aspect of your query makes it pretty complicated.

The PIVOT function is nice, but it requires that you know the resulting columns when you build the SQL. So, what you need to is create a stored proc that dynamically builds the PIVOT sql syntax by looping through the data in your table.

I know of a couple of good articles with examples of doing this (all-be-it using the old SQL 2000 SQL pivot logic).

A good article on simple PIVOTS. http://www.aspfaq.com/show.asp?id=2462

That article has a link to 2 dynamic pivot articles. I think the first is the best for you. http://www.sqlmag.com/Articles/Index.cfm?ArticleID=15608

If I were you, I'd consider re-writing this for the new PIVOT function, and perhaps someone here already has. If this doesn't work, I could probably find some time to do this for you. Let me know. But, I think just using the old version described in this article should work.

|||

Thanks again, Ryan! You have a wealth of knowledge.

I am doing this for a project that needs to be ready this Wednesday, so I may not have enough time to construct this myself by then. However, I am loathe to have you do my work for me. So, I will probably do a quick and dirty view on the data and over the next week or so wrap my mind around your links and other advice. If I do need further advice I will post to this thread and hope you check back now and then.

Thanks for everything!

|||

Hi there Ryan,

I've seen you've gave a solution in case of SQL Server 2005. Do you have a solution for 2000?

Thanks in advance

|||You can have the best example here:
http://orafaq.com/node/1871|||

Code Snippet

If you use SQL Server 2005,

use tempdb

go

create table data (

[date] datetime ,

[ordernum] varchar(100) ,

[workday] int

);

go

insert into data values('2006-06-1','AA','1');

insert into data values('2006-06-1','AB','1');

insert into data values('2006-06-2','BA','2');

insert into data values('2006-06-2','BB','2');

insert into data values('2006-06-2','BC','2');

insert into data values('2006-06-5','CA','3');

insert into data values('2006-06-5','CB','3');

go

;with cte

as

(

select *, row_number() over(partition by date order by ordernum) rid from data

)

select

isnull([1],'') as day1

, isnull([2],'') as day2

, isnull([3],'') as day3

, isnull([4],'') as day4

, isnull([5],'') as day5

, isnull([6],'') as day6

, isnull([7],'') as day7

from

(

select

workday

, rid

, ordernum

from

cte

) as data

pivot

(

max(ordernum)

for workday in

([1],[2],[3],[4],[5],[6],[7] /*.....,[n]*/)

)as pvt

Attempting to transpose/pivot columns to rows... I think.

I have something of a challenge for you folks (at least it's very challenging for me).

I have a table that has data that looks like this:

DateOrderNum_WorkDay

2006-06-1__AA1

2006-06-1__AB1

2006-06-2__BA2

2006-06-2__BB2

2006-06-2__BC2

2006-06-5__CA3

2006-06-5__CB3

etc.

So, there are dates that orders happened and each date is marked as the 1, 2, 3, etc. business day of the month.

What I am trying to write is a SQL statement that will display results thus:

Day1Day2Day3_etc.

AA__BA_CAetc.

AB__BB_CBetc.

Is this making any sense to anyone? Basically, I need to turn the WorkDay rows into columns and display all the info for each WorkDay under them.

I have a feeling this isn't hard, but I am fairly new to writing SQL so any advice would be pure gold to me. Thanks!

2 questions.

1 - What version of SQL Server are you using? Is it 2005?

2- Is the set of Work days dynamic, or do you know the specific work days when you are writing the query. More specifically, do the columns in the result need to be generated dynamically from the data in the table?

|||

Ryan,

Thanks for the quick reply! Here are the answers to your questions:

1. I'm using SQL Server 2005 (and I just found the PIVOT command which is new in 2005 apparently).

2. The set of workdays is dynamic. Depending on the size of the month and holidays there could be as few as 20 or as many as 23. And if I understand your second question then yes the columns will need to be generated dynammically. The data in the table will be changing daily and this SQL statement is generating a grid view on a web page.

Thanks again!

|||

Unfortunately, the dynamic aspect of your query makes it pretty complicated.

The PIVOT function is nice, but it requires that you know the resulting columns when you build the SQL. So, what you need to is create a stored proc that dynamically builds the PIVOT sql syntax by looping through the data in your table.

I know of a couple of good articles with examples of doing this (all-be-it using the old SQL 2000 SQL pivot logic).

A good article on simple PIVOTS. http://www.aspfaq.com/show.asp?id=2462

That article has a link to 2 dynamic pivot articles. I think the first is the best for you. http://www.sqlmag.com/Articles/Index.cfm?ArticleID=15608

If I were you, I'd consider re-writing this for the new PIVOT function, and perhaps someone here already has. If this doesn't work, I could probably find some time to do this for you. Let me know. But, I think just using the old version described in this article should work.

|||

Thanks again, Ryan! You have a wealth of knowledge.

I am doing this for a project that needs to be ready this Wednesday, so I may not have enough time to construct this myself by then. However, I am loathe to have you do my work for me. So, I will probably do a quick and dirty view on the data and over the next week or so wrap my mind around your links and other advice. If I do need further advice I will post to this thread and hope you check back now and then.

Thanks for everything!