Showing posts with label project. Show all posts
Showing posts with label project. Show all posts

Friday, March 30, 2012

Relationships: Cascade delete

Hi,

I Use SQL Server 2005 Express edition. I have a few tables, which are inter-related. For e.g. I have a project table (ProjectID, Name, ...)

I also have a Project invoice table, in which ProjectID is referred as Foreign key.

Now, I want all the rows in Project invoice (child) table table to be deleted (for a particular ProjetID), if I delete the coresponding ProjectID in Projects (parent) table. If I use the 'on delete set null' or 'on delete cascade' constraint, only the ProjectID (in child table) is nullified whereas rest of the columns have data.

Is there any way out, where the whole ROW (not just that field) is deleted? Or, is it that this can be achieved only by some stored procedure / external program?

Thanks!

Hi !

That is done using "On cascade delete" option. The Null option does the effect you described first, setting the foreign key field to Null. Seems that you have an old vesion of the foreign key in place. Drop the Constraint and recreate it using the "on delete cascade" keywords and you will see that the row will be deleted.

HTH, Jens K. Suessmeyer.


http://www.sqlserver2005.de

relationships in Project server2k3-> reporting services?

Ok,
We have setup SQL2005 reporting services with MOSS integration. I have
created a datasource that points at our Project server 2003 SQL
database so that we can start to create project server reports. The
data source works just fine BUT its *doesnt* pick up any relationships
between tables from the Project2k3 SQL database.
This means that when you create a report builder report the first
table you selct becomes the primary and no others are visable :( not
very useful for creating meaningful reports.
Questions:
a) How does SQL2005RS read the relationships between table entities in
a remote DB so that they can be used together within the report
builder?
b) Does anyone have recommendations, URLs, How-tos on setting up
reporting services against Project server 2003?
Tips welcome
Al BlakeI have written many reports against the Project Server 2003 database. I have
found there are not relationships in the Project Server database. You always
have to create your own joins of the data.
Most of the time I will create a query using query analyzer, Then use SRS
to group ,filter and sum the data.
Hope this helps
--
Ray McCoppin
http://www.randsmanagement.com
Project Server reports.
"scubaal" wrote:
> Ok,
> We have setup SQL2005 reporting services with MOSS integration. I have
> created a datasource that points at our Project server 2003 SQL
> database so that we can start to create project server reports. The
> data source works just fine BUT its *doesnt* pick up any relationships
> between tables from the Project2k3 SQL database.
> This means that when you create a report builder report the first
> table you selct becomes the primary and no others are visable :( not
> very useful for creating meaningful reports.
> Questions:
> a) How does SQL2005RS read the relationships between table entities in
> a remote DB so that they can be used together within the report
> builder?
> b) Does anyone have recommendations, URLs, How-tos on setting up
> reporting services against Project server 2003?
> Tips welcome
> Al Blake
>|||Thanks for the feedback. I thought that was the case but wanted be
sure I hadnt missed something.
I am quite comfortable in making the queries through SQL QA until I
get the right result but then I am unclear as to how I get this into
the SQL2005RS environment through MOSS.
Are you saying I should define views within the SQL database and then
use report builder to make a report against that?
OR
is there some way to 'parameterise' the Report Model object when you
create it to use a SQL queiry with join as the basis of its model?
As far as I can see when I create a RB model the ONLY option I have is
the datasource I want to use......there doesnt seem to be any way to
create an RBM from a datasourse using
SELECT * FROM A,B WHERE A.ID=B.ID
or am I missing something again?
Regards

Relationships in MSDE - how do I do it?

Hi
I am using the Web Matrix Project to write a .NETapplication. I have set up a database using MSDE which Iaccessed through the Web Matrix Project to start with. I have nowfound the Web Data Administrator which I use to backup my database andhelp to transfer it to the Server where I hope to run this application(if I ever get it finished!).
I need to set up some relationships between database tables but cannotsee a way to do this. SQL Server Books on Line referes toEnterprise Manager which I believe ships with the full MS SQL Serverpackage (and costs a package). I am doing this on ashoe-string. Any ideas on how to do it?
Thanks in advance.
Mike
CREATE TABLE order_part
(order_nmbr int,
part_nmbr int
FOREIGN KEY REFERENCES part_sample(part_nmbr)
ON DELETE NO ACTION,
qty_ordered int)
GO

you could do an alter table if its already there|||Hi DeveloperMCDBA
I did not realise I could create a table using a StoredProcedure. It certainly takes me longer than just using theWebMatrix but I seem to be getting there. Many thanks for thehelp - much appreciated.
Mike

Tuesday, March 20, 2012

re-insert / templating records

I have a problem. The project that I'm working on requires me to duplicate
records. As in a series of records are entered into the system the user then
click on a button to make these as 'Templates' so that they would not have
to re-enter alot of the information. So from a db perspective I would have
to re-insert these records into the database. The problem is there're over
20 tables and the relationship is complex. Is there a way to copy the parent
the record and have sqlserver automatically cascade and re-insert all
related and referenced records back into the database ?

or is there a easy way to do this ?

Thanks
TomDuplicating rows in a table should never be necessary or desirable and it
shouldn't even be possible since evey table should have unique/primary key
constraints that prevent this. I assume therefore you will want to maintain
uniqueness by changing some column values. Unfortunately you haven't told us
anything about keys, constraints or the data you want to modify.

> Is there a way to copy the parent
> the record and have sqlserver automatically cascade and re-insert all
> related and referenced records back into the database ?

I guess here that you are talking about copying rows between tables with
IDENTITY columns. This is easy provided you have declared natural (not
IDENTITY) keys on the tables. IDENTITY should not be the only key of a
table. Here is an example of moving a parent entity and its related rows
between tables while maintaining the surrogate keys.

CREATE TABLE Departments (deptid INTEGER IDENTITY PRIMARY KEY, deptname
VARCHAR(30) NOT NULL UNIQUE /* Note the Key */)

CREATE TABLE Employees (employeeid INTEGER IDENTITY PRIMARY KEY, ssn
CHAR(10) NOT NULL UNIQUE /* Note the Key */, employeename VARCHAR(30) NOT
NULL, deptid INTEGER NOT NULL REFERENCES Departments (deptid))

CREATE TABLE New_Departments (deptid INTEGER IDENTITY PRIMARY KEY, deptname
VARCHAR(30) NOT NULL UNIQUE)

CREATE TABLE New_Employees (employeeid INTEGER IDENTITY PRIMARY KEY, ssn
CHAR(10) NOT NULL UNIQUE, employeename VARCHAR(30) NOT NULL, deptid INTEGER
NOT NULL REFERENCES New_Departments (deptid))

INSERT INTO New_Departments (deptname)
SELECT D.deptname
FROM Departments AS D
LEFT JOIN New_Departments AS N
ON D.deptname = N.deptname
WHERE N.deptname IS NULL

INSERT INTO New_Employees (ssn, employeename, deptid)
SELECT E1.ssn, E1.employeename, D2.deptid
FROM Employees AS E1
JOIN Departments AS D1
ON E1.deptid = D1.deptid
JOIN New_Departments AS D2
ON D1.deptname = D2.deptname
LEFT JOIN New_Employees AS E2
ON E1.ssn = E2.ssn
WHERE E2.employeeid IS NULL

--
David Portas
SQL Server MVP
--|||Tom Gao (tomgaomail@.optushome.com.au) writes:
> I have a problem. The project that I'm working on requires me to
> duplicate records. As in a series of records are entered into the system
> the user then click on a button to make these as 'Templates' so that
> they would not have to re-enter alot of the information. So from a db
> perspective I would have to re-insert these records into the database.
> The problem is there're over 20 tables and the relationship is complex.
> Is there a way to copy the parent the record and have sqlserver
> automatically cascade and re-insert all related and referenced records
> back into the database ?

There's a whole lot of information missing here, but in any case, the
answer is: no.

Are you inserting into the same table, or from a table with templates?
Well, in most cases it makes sense to store templates in the same table
as the real rows.

But then there are at least two columns that are not to be copied to
the new rows: the key and the column that marks that the template is a
template.

And I would not be surprised if there are more columns. For instance,
say that there are auditing columns who tells which which user that
created the row and when. Such data is of course not possible to
inherit from the client.

So, I am sorry, you just have to start coding. And pay attention to
the business requirements, so that you copy what you should copy, no
more, no less.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Reinitializing SqlCommand object

Hi All,
Can the same connection object and the command object be reinitialised and
re-used through-out the project? Is the code below permissible?
Dim DBConnection As SqlConnection
Dim oCmd As SqlCommand
Dim sStmt As String
DBConnection.Open()
sStmt = "select count(*)......"
oCmd = New SqlCommand(sStmt, DBConnection)
oCmd.CommandText = CommandType.Text
ocmd.ExecuteScalar()
.....
...
...
sStmt = "insert ..."
oCmd = New SqlCommand(sStmt, DBConnection)
oCmd.CommandText = CommandType.Text
DBConnection.Open()
ocmd.ExecuteNonQuery()
.....
...
...
sStmt = "update......"
oCmd = New SqlCommand(sStmt, DBConnection)
oCmd.CommandText = CommandType.Text
DBConnection.Open()
ocmd.ExecuteNonQuery()
.....
...
...
oCmd.Connection.Close()
Would previous instances of SqlCommand object be garbage collected
automatically?
kdkd,
This is a .NET question, not SQL Server, but I'll field it.

> Can the same connection object and the command object be
> reinitialised and re-used through-out the project?
First off, in your code, you are not reusing the *object* you are reusing
the *reference* (the variable name) . There is a difference.
Second, you say "through-out the project" which to me implies global usage.
That's a bad, bad, bad practice. Declare, instantiate, Open, Use, and then
Close your connection only when you need it. No more.
If you have a series of commands to execute one after another, the pattern I
would recommend is this:
Dim myCommand as SqlCommand
Dim myConnection as New SqlConnection(ConnectionString)
myConnection.Open()
myCommand = New SqlCommand(sql,myConnection)
myCommand.ExecuteNonQuery
myCommand = New SqlCommand(sql,myConnection)
myCommand.ExecuteNonQuery
...
myConnection.Close()

> Would previous instances of SqlCommand object be
> garbage collected automatically?
Garbage collection in .NET is not immediate. It's "when it gets to it." But
yes, it will be collected. "myConnection = nothing" is uneeded and not
recommended.
Even though a connection is automatically closed when GC'ed, I still
recommend an explicit "myConnection.Close()." This will free up the ADO.NET
connection pool.
And one final note, consider looking at MSDN naming guidelines. In .NET, we
do not use "sStmt" or "oCmd".
--Alex Papadimoulis
"kd" wrote:

> Hi All,
> Can the same connection object and the command object be reinitialised and
> re-used through-out the project? Is the code below permissible?
> Dim DBConnection As SqlConnection
> Dim oCmd As SqlCommand
> Dim sStmt As String
> DBConnection.Open()
> sStmt = "select count(*)......"
> oCmd = New SqlCommand(sStmt, DBConnection)
> oCmd.CommandText = CommandType.Text
> ocmd.ExecuteScalar()
> ......
> ....
> ....
> sStmt = "insert ..."
> oCmd = New SqlCommand(sStmt, DBConnection)
> oCmd.CommandText = CommandType.Text
> DBConnection.Open()
> ocmd.ExecuteNonQuery()
> ......
> ....
> ....
> sStmt = "update......"
> oCmd = New SqlCommand(sStmt, DBConnection)
> oCmd.CommandText = CommandType.Text
> DBConnection.Open()
> ocmd.ExecuteNonQuery()
> ......
> ....
> ....
> oCmd.Connection.Close()
> Would previous instances of SqlCommand object be garbage collected
> automatically?
> kd|||Hi Alex,
Thanks for the guidelines.
Regards,
kd
"Alex Papadimoulis" wrote:
> kd,
> This is a .NET question, not SQL Server, but I'll field it.
>
> First off, in your code, you are not reusing the *object* you are reusing
> the *reference* (the variable name) . There is a difference.
> Second, you say "through-out the project" which to me implies global usage
.
> That's a bad, bad, bad practice. Declare, instantiate, Open, Use, and then
> Close your connection only when you need it. No more.
> If you have a series of commands to execute one after another, the pattern
I
> would recommend is this:
> Dim myCommand as SqlCommand
> Dim myConnection as New SqlConnection(ConnectionString)
> myConnection.Open()
> myCommand = New SqlCommand(sql,myConnection)
> myCommand.ExecuteNonQuery
> myCommand = New SqlCommand(sql,myConnection)
> myCommand.ExecuteNonQuery
> ...
> myConnection.Close()
>
> Garbage collection in .NET is not immediate. It's "when it gets to it." Bu
t
> yes, it will be collected. "myConnection = nothing" is uneeded and not
> recommended.
> Even though a connection is automatically closed when GC'ed, I still
> recommend an explicit "myConnection.Close()." This will free up the ADO.NE
T
> connection pool.
> And one final note, consider looking at MSDN naming guidelines. In .NET, w
e
> do not use "sStmt" or "oCmd".
> --Alex Papadimoulis
>
> "kd" wrote:
>|||Hi,
Would you be able to provide links for naming guidelines in .NET?
Thanks
kd
"Alex Papadimoulis" wrote:
> kd,
> This is a .NET question, not SQL Server, but I'll field it.
>
> First off, in your code, you are not reusing the *object* you are reusing
> the *reference* (the variable name) . There is a difference.
> Second, you say "through-out the project" which to me implies global usage
.
> That's a bad, bad, bad practice. Declare, instantiate, Open, Use, and then
> Close your connection only when you need it. No more.
> If you have a series of commands to execute one after another, the pattern
I
> would recommend is this:
> Dim myCommand as SqlCommand
> Dim myConnection as New SqlConnection(ConnectionString)
> myConnection.Open()
> myCommand = New SqlCommand(sql,myConnection)
> myCommand.ExecuteNonQuery
> myCommand = New SqlCommand(sql,myConnection)
> myCommand.ExecuteNonQuery
> ...
> myConnection.Close()
>
> Garbage collection in .NET is not immediate. It's "when it gets to it." Bu
t
> yes, it will be collected. "myConnection = nothing" is uneeded and not
> recommended.
> Even though a connection is automatically closed when GC'ed, I still
> recommend an explicit "myConnection.Close()." This will free up the ADO.NE
T
> connection pool.
> And one final note, consider looking at MSDN naming guidelines. In .NET, w
e
> do not use "sStmt" or "oCmd".
> --Alex Papadimoulis
>
> "kd" wrote:
>|||kd,
[url]http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconnamingguidelines.asp[/u
rl]
-- Alex Papadimoulis
"kd" wrote:
> Hi,
> Would you be able to provide links for naming guidelines in .NET?
> Thanks
> kd
> "Alex Papadimoulis" wrote:
>

Monday, February 20, 2012

registering a sql server

i'm the first time to use sql server 2000 to develop my
final year project....since i never create any database
using sql server 2000 before...my "sql server group" is
empty......when i register a server....a message
prompted out as folling "SqlConnection()is failed or
access denied"......what i have to do in order to
register a server successfully
Hi,
Are you trying to register from the machine you have installed SQL Server,
If yes then add the server name as
"LOCAL". Use the SQL Server Authentication, then give
login name as "SA" and the password ( SHould be blank) and continue
registration.
If "Local" is not allowing then try identifying the SQL Server name by
opening the SQL error log in \log folder of sql server installation. Use
that server name to register.
Thanks
Hari
MCDBA
"chan" <chanling10@.time.net.my> wrote in message
news:11ab601c44210$e8320a40$a301280a@.phx.gbl...
> i'm the first time to use sql server 2000 to develop my
> final year project....since i never create any database
> using sql server 2000 before...my "sql server group" is
> empty......when i register a server....a message
> prompted out as folling "SqlConnection()is failed or
> access denied"......what i have to do in order to
> register a server successfully

registering a sql server

i'm the first time to use sql server 2000 to develop my
final year project....since i never create any database
using sql server 2000 before...my "sql server group" is
empty......when i register a server....a message
prompted out as folling "SqlConnection()is failed or
access denied"......what i have to do in order to
register a server successfullyHi,
Are you trying to register from the machine you have installed SQL Server,
If yes then add the server name as
"LOCAL". Use the SQL Server Authentication, then give
login name as "SA" and the password ( SHould be blank) and continue
registration.
If "Local" is not allowing then try identifying the SQL Server name by
opening the SQL error log in \log folder of sql server installation. Use
that server name to register.
Thanks
Hari
MCDBA
"chan" <chanling10@.time.net.my> wrote in message
news:11ab601c44210$e8320a40$a301280a@.phx
.gbl...
> i'm the first time to use sql server 2000 to develop my
> final year project....since i never create any database
> using sql server 2000 before...my "sql server group" is
> empty......when i register a server....a message
> prompted out as folling "SqlConnection()is failed or
> access denied"......what i have to do in order to
> register a server successfully