Showing posts with label user. Show all posts
Showing posts with label user. Show all posts

Thursday, March 29, 2012

Deleting rows from many tables

What I'm trying to do is delete a user and all their related information within the other tables. I'm not wanting to delete the table, just one column with that user and their related information. So my Primary_Key is UserID within the table [alumni] and my three Foreign_Keys are CommentID, PhotoID, and AlbumID within the tables [comments], [photos], and [albums]. Here is some of the code that I have:

<asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="<%$ ConnectionStrings:SoderquistString %>"DeleteCommand="DELETE FROM [alumni] WHERE [UserID] = @.UserID"SelectCommand="SELECT [UserID], [UserName], [FirstName], [LastName], [State] FROM [alumni] WHERE ([State] = @.State)"><DeleteParameters><asp:ParameterName="UserID"Type="Int32"/></DeleteParameters><SelectParameters><asp:ControlParameterControlID="DropDownList1"Name="state"PropertyName="SelectedValue"Type="String"/></SelectParameters></asp:SqlDataSource>

The users are set up in GridView form. Is there some type of DELETE command that I need to be writing that is different than the one above? I have tried adding onto the following DELETE statment:

DeleteCommand="DELETE FROM [alumni] WHERE [UserID] = @.UserID

DELETE FROM [photo] WHERE [UserID] = @.UserID;

DELETE FROM [album] WHERE [UserID] = @.UserID;

DELETE FROM [comment] WHERE [UserID] = @.UserID;

...but that doesn't work...and doesn't look right. I would really appreciate anyones suggestions or help that you may be able to provide. Thank you!

Try this. I tested for two tables

DeleteCommand="DELETE FROM [alumni] WHERE [UserID] = @.UserID;DELETE FROM [photo] WHERE [UserID] = @.UserID;DELETE FROM [album] WHERE [UserID] = @.UserID;DELETE FROM [comment] WHERE [UserID] = @.UserID"

|||

Thanks for the info. I've tried that already and I still get an error:

Invalid column name 'UserID'.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.Data.SqlClient.SqlException: Invalid column name 'UserID'.

Any suggestions?

|||

Do you have UserID column for all tables you are going to delete?

Since I tested the syntax for only two tables, I will test for more tables to confirm whether the syntax works for multiple tables.

|||

This DeleteCommand works on the dummy test tables in SQL Server 2005:

DeleteCommand="DELETE FROM [T1] WHERE [ID1] = @.ID;DELETE FROM [T2] WHERE [ID1] = @.ID;DELETE FROM [T3] WHERE [ID1] = @.ID;DELETE FROM [T4] WHERE [ID1] = @.ID;DELETE FROM [T5] WHERE [ID1] = @.ID"<DeleteParameters><asp:ParameterName="ID"Type="Int32"/></DeleteParameters>

But if you click delete rows too fast, you may get error message for the parameter. You should be able to delete from a stored procedure with error checking from there.

|||

Limno, thanks and I appreciate the help you have given me. I was looking over my tables and noticed that there is a difference in one of them. I'm going to write out the four tables and a few columns that I have within them and hopefully this will help clear a few things up that I'm having trouble with:

alumni - UserID (PK), FirstName, LastName, UserName

photo - PhotoID (FK), AlbumID, PhotoPath

album - AlbumID (FK), UserID, PhotoPath, AlbumName

comments - CommentID (FK), UserID, Comment, UserName, CommentDate

What I have is one primary_key, which is UserID and the other three are foreign_keys (PhotoID, AlbumID, and CommentID). All of the foreign keys have UserID in them except for the photo table because it associates with the album table that has UserID in it. That was the one difference that I forgot about.

So is it possible, with the way I have this set up for me to be able to delete a user from the alumni table along with the other information in the other tables that are associated with that user? I can delete a user if there are no albums, photos or comments connected with that person, but it's when they have something connected to them a problem comes up because of certain constraints.

Thank you in advanced!

|||

Hello:

I tested with the following DELETECommand:

DeleteCommand="DELETE FROM [photo] WHERE ([phtoID] IN (SELECT photoID FROM album WHERE userID = @.userID)); DELETE FROM [comments] WHERE [userID] = @.userID;DELETE FROM [alumni] WHERE [userID] = @.userID;DELETE FROM [album] WHERE [userID] = @.userID"

It seems that the cascading delete through a trigger would be a perfect fit for your case. However, I rearrange the delete action sequence for your case in this DeleteCommand. I tested with success. But you need to test on your data to make sure it works properly. If you want to control the flow, you may need to go with the trigger and/or stored procedure to handle your multiple cascading deletion.

|||

It worked! Thank you for your time, I really appreciate it. It looks like my problem was with the way I was deleting the information from the photo table and how it was related to the albums. This peace of code here...

DELETE FROM [photo] WHERE ([phtoID] IN (SELECT photoID FROM album WHERE userID = @.userID));

...did the trick.

Just for my understanding, what did you mean by "If you want to control the flow, you may need to go with the trigger and/or stored procedure to handle your multiple cascading deletion"? Thanks again!

|||

The code works but you have no way to know if something wrong with it during deletion. I did run into a few times during my test. That is what I mean. If the project becomes critical, you can go with the trigger and stored procedure. Or You can use your own error catch logic from code behind. Just my thought for your situation. I want to know what will happen if there is an error for learning purpose.

I am glad this hack works for you now. If you are interested, you can search for the topic on cascading deleting. The constrains on the foreign keys are very usful to maintain the integrity of your data set.

Wednesday, March 21, 2012

Deleting Data From DB

I have edited the aspnet_Users_CreateUser stored procedure so that the UserId that is created when a new user is created is copied to a UserId field in another table. However, when I use the website administration tool to delete users that have been created, it gives me an error saying the delete statement conflicted with the reference constraint. I then added the following code in the aspnet_Users_DeleteUser procedure...

IF((@.TablesToDeleteFrom & 16) <> 0AND

(

EXISTS(SELECT name FROMsysobjectsWHERE(name= N'vw_tt')AND(type ='V'))))BEGINDELETE FROMdbo.userclasssetWHERE@.UserId = UserIdSELECT@.ErrorCode = @.@.ERROR,

@.RowCount = @.@.ROWCOUNT

IF( @.ErrorCode <> 0 )GOTOCleanupIF(@.RowCount <> 0)SELECT@.NumTablesDeletedFrom = @.NumTablesDeletedFrom + 1 hdhd

END

This code was then added to the function at the end which deletes the data from the aspnet_Users table when everything else has been removed

(@.TablesToDeleteFrom & 16) <> 0

AND

Now when I delete a user in the website admin tool, it "deletes" (with no error) the user from the list but doesnt actually physically delete it from the database.

Any ideas?

Problem fixed. The following code seemed to do the trick. I just needed to place it in the right point in the code which was just before the deletion of the UserId information in the membership table. Sorry to waste peoples time!

DELETE FROM dbo.userclassset WHERE @.UserId = UserId

Deleting and Updating from Gridview

Hi,

I made a gridview, and I am trying to make it so when the user deletes a row, values in other tables update. I used the following source code:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [Transactions] WHERE [TransactionID] = @.TransactionIDAND UPDATE [items] SET Quantityavailable, numtaken VALUES Quantityavailable + 1, numtaken - 1 WHERE ([Itemid] = @.Itemid) "

It gives the error that Quantityavailable is not a SET type?

Thanks if you can suggest a remedy!

Jon

jbear123:

UPDATE [items] SET Quantityavailable, numtaken VALUES Quantityavailable + 1, numtaken - 1 WHERE ([Itemid] = @.Itemid

That is not valid syntax for an UPDATE statement.

Try this

UPDATE itemsSET Quantityavailable = Quantityavailable + 1, numtaken = numtaken - 1WHERE ItemId = @.ItemId
|||

Thanks!

Monday, March 19, 2012

Deleting a UDF that does not exist...

I tried to grant a user (harry) rights to a UDF (xxxyyyzzz) by using this
T-SQL command in a script:
GRANT EXEC on xxxyyyzzz to harry
problem is, there was no xxxyyyzzz UDF or SP in the database. Now when I
highlight this or any user on this database and try to click the Permissions
button, I get error:
Microsoft SQL-DMO
Error 21776: [SQL-DMO]The name 'xxxyyyzzz' was not found in the
UserDefinedFunctions collection. If the name is a qualified name, use [] to
separate various parts of the name, and try again.
how can I delete this "phantom" object? If I scroll through all the objects
that this user has rights to, I can see the xxxyyyzzz UDF, but I can't delete
it.
Please help. Thanks.
OK. Located it in the systemobjects table, but when I try to delet that row,
get an error message:
Ad hoc updates to the system catalogs are not enabled. The system
administrator must reconfigure SQL server to allow this.
Help again.
"Sam" wrote:

> I tried to grant a user (harry) rights to a UDF (xxxyyyzzz) by using this
> T-SQL command in a script:
> GRANT EXEC on xxxyyyzzz to harry
> problem is, there was no xxxyyyzzz UDF or SP in the database. Now when I
> highlight this or any user on this database and try to click the Permissions
> button, I get error:
> --
> Microsoft SQL-DMO
> --
> Error 21776: [SQL-DMO]The name 'xxxyyyzzz' was not found in the
> UserDefinedFunctions collection. If the name is a qualified name, use [] to
> separate various parts of the name, and try again.
> how can I delete this "phantom" object? If I scroll through all the objects
> that this user has rights to, I can see the xxxyyyzzz UDF, but I can't delete
> it.
> Please help. Thanks.
|||Never mind - was able to delete it from the sysobjects table using:
sp_configure 'allow updates; ,1
reconfigure with overrride
all's well that ends well. no more error message
"Sam" wrote:

> I tried to grant a user (harry) rights to a UDF (xxxyyyzzz) by using this
> T-SQL command in a script:
> GRANT EXEC on xxxyyyzzz to harry
> problem is, there was no xxxyyyzzz UDF or SP in the database. Now when I
> highlight this or any user on this database and try to click the Permissions
> button, I get error:
> --
> Microsoft SQL-DMO
> --
> Error 21776: [SQL-DMO]The name 'xxxyyyzzz' was not found in the
> UserDefinedFunctions collection. If the name is a qualified name, use [] to
> separate various parts of the name, and try again.
> how can I delete this "phantom" object? If I scroll through all the objects
> that this user has rights to, I can see the xxxyyyzzz UDF, but I can't delete
> it.
> Please help. Thanks.

Deleting a UDF that does not exist...

I tried to grant a user (harry) rights to a UDF (xxxyyyzzz) by using this
T-SQL command in a script:
GRANT EXEC on xxxyyyzzz to harry
problem is, there was no xxxyyyzzz UDF or SP in the database. Now when I
highlight this or any user on this database and try to click the Permissions
button, I get error:
Microsoft SQL-DMO
Error 21776: [SQL-DMO]The name 'xxxyyyzzz' was not found in the
UserDefinedFunctions collection. If the name is a qualified name, use [
] to
separate various parts of the name, and try again.
how can I delete this "phantom" object? If I scroll through all the objects
that this user has rights to, I can see the xxxyyyzzz UDF, but I can't delet
e
it.
Please help. Thanks.OK. Located it in the systemobjects table, but when I try to delet that row,
get an error message:
Ad hoc updates to the system catalogs are not enabled. The system
administrator must reconfigure SQL server to allow this.
Help again.
"Sam" wrote:

> I tried to grant a user (harry) rights to a UDF (xxxyyyzzz) by using this
> T-SQL command in a script:
> GRANT EXEC on xxxyyyzzz to harry
> problem is, there was no xxxyyyzzz UDF or SP in the database. Now when I
> highlight this or any user on this database and try to click the Permissio
ns
> button, I get error:
> --
> Microsoft SQL-DMO
> --
> Error 21776: [SQL-DMO]The name 'xxxyyyzzz' was not found in the
> UserDefinedFunctions collection. If the name is a qualified name, use
1;] to
> separate various parts of the name, and try again.
> how can I delete this "phantom" object? If I scroll through all the object
s
> that this user has rights to, I can see the xxxyyyzzz UDF, but I can't del
ete
> it.
> Please help. Thanks.|||Never mind - was able to delete it from the sysobjects table using:
sp_configure 'allow updates; ,1
reconfigure with overrride
all's well that ends well. no more error message
"Sam" wrote:

> I tried to grant a user (harry) rights to a UDF (xxxyyyzzz) by using this
> T-SQL command in a script:
> GRANT EXEC on xxxyyyzzz to harry
> problem is, there was no xxxyyyzzz UDF or SP in the database. Now when I
> highlight this or any user on this database and try to click the Permissio
ns
> button, I get error:
> --
> Microsoft SQL-DMO
> --
> Error 21776: [SQL-DMO]The name 'xxxyyyzzz' was not found in the
> UserDefinedFunctions collection. If the name is a qualified name, use
1;] to
> separate various parts of the name, and try again.
> how can I delete this "phantom" object? If I scroll through all the object
s
> that this user has rights to, I can see the xxxyyyzzz UDF, but I can't del
ete
> it.
> Please help. Thanks.

Deleting a UDF that does not exist...

I tried to grant a user (harry) rights to a UDF (xxxyyyzzz) by using this
T-SQL command in a script:
GRANT EXEC on xxxyyyzzz to harry
problem is, there was no xxxyyyzzz UDF or SP in the database. Now when I
highlight this or any user on this database and try to click the Permissions
button, I get error:
--
Microsoft SQL-DMO
--
Error 21776: [SQL-DMO]The name 'xxxyyyzzz' was not found in the
UserDefinedFunctions collection. If the name is a qualified name, use [] to
separate various parts of the name, and try again.
how can I delete this "phantom" object? If I scroll through all the objects
that this user has rights to, I can see the xxxyyyzzz UDF, but I can't delete
it.
Please help. Thanks.OK. Located it in the systemobjects table, but when I try to delet that row,
get an error message:
Ad hoc updates to the system catalogs are not enabled. The system
administrator must reconfigure SQL server to allow this.
Help again.
"Sam" wrote:
> I tried to grant a user (harry) rights to a UDF (xxxyyyzzz) by using this
> T-SQL command in a script:
> GRANT EXEC on xxxyyyzzz to harry
> problem is, there was no xxxyyyzzz UDF or SP in the database. Now when I
> highlight this or any user on this database and try to click the Permissions
> button, I get error:
> --
> Microsoft SQL-DMO
> --
> Error 21776: [SQL-DMO]The name 'xxxyyyzzz' was not found in the
> UserDefinedFunctions collection. If the name is a qualified name, use [] to
> separate various parts of the name, and try again.
> how can I delete this "phantom" object? If I scroll through all the objects
> that this user has rights to, I can see the xxxyyyzzz UDF, but I can't delete
> it.
> Please help. Thanks.|||Never mind - was able to delete it from the sysobjects table using:
sp_configure 'allow updates; ,1
reconfigure with overrride
all's well that ends well. no more error message
"Sam" wrote:
> I tried to grant a user (harry) rights to a UDF (xxxyyyzzz) by using this
> T-SQL command in a script:
> GRANT EXEC on xxxyyyzzz to harry
> problem is, there was no xxxyyyzzz UDF or SP in the database. Now when I
> highlight this or any user on this database and try to click the Permissions
> button, I get error:
> --
> Microsoft SQL-DMO
> --
> Error 21776: [SQL-DMO]The name 'xxxyyyzzz' was not found in the
> UserDefinedFunctions collection. If the name is a qualified name, use [] to
> separate various parts of the name, and try again.
> how can I delete this "phantom" object? If I scroll through all the objects
> that this user has rights to, I can see the xxxyyyzzz UDF, but I can't delete
> it.
> Please help. Thanks.

Deleting a record

I have three tables

1. membership table(aspnet_membership table)

2. User Contacts table

3. Address table

These three table have relation ship with one another through a UserId field.

How will you set up cascaded delete on these tables, Like if I delete a user in the membership table I want the related records in the other tables to be deleted as well. Cascaded delete is it something done through code, or is it definde when the tables are created.

Please advice.

bendJoe:

is it definde when the tables are created.

Yes, you should define it when you create the tables. You can also use theALTER TABLE command if you want to add to existing tables.

Friday, March 9, 2012

deleted all records strangely

we have two tables: TIMECARDBATCH and TIMECARD. TIMECARD has a foreign key
to TIMECARDBATCH.
User can only delete one row from TIMECARDBATCH at a time. We have a delete
trigger on TIMECARDBATCH which deletes corresponding records from TIMECARD.
In our production environment at one time all records in TIMECARD gets
deleted but there are still records in TIMECARD. Now we look into the code
and the programmer whoe code the delete trigger has this code:
CREATE TRIGGER td_timecardBatch ON [dbo].[TIMECARDBATCH]
FOR DELETE
AS
declare @.batchNumber int
declare @.id int
select @.batchNumber = batchNumber
from deleted
declare c_tc cursor local for
select id from timecard where batchNumber=@.batchNumber
open c_tc
fetch c_tc into @.id
while @.@.fetch_status=0
begin
delete from timecard where id=@.id
fetch c_tc into @.id
end
What I can see wrong here is that the cursor "c_tc" does not get closed and
deallocated. Can this cause the problem?
Thanksone more question i have is :
does SQL Server log SQL statements for a database? If yes then where can i
find them?
Thanks
"JY" <jy1970us@.yahoo.com> wrote in message
news:BVKef.2604$w84.470302@.news20.bellglobal.com...
> we have two tables: TIMECARDBATCH and TIMECARD. TIMECARD has a foreign key
> to TIMECARDBATCH.
> User can only delete one row from TIMECARDBATCH at a time. We have a
> delete trigger on TIMECARDBATCH which deletes corresponding records from
> TIMECARD.
> In our production environment at one time all records in TIMECARD gets
> deleted but there are still records in TIMECARD. Now we look into the code
> and the programmer whoe code the delete trigger has this code:
> CREATE TRIGGER td_timecardBatch ON [dbo].[TIMECARDBATCH]
> FOR DELETE
> AS
> declare @.batchNumber int
> declare @.id int
> select @.batchNumber = batchNumber
> from deleted
> declare c_tc cursor local for
> select id from timecard where batchNumber=@.batchNumber
> open c_tc
> fetch c_tc into @.id
> while @.@.fetch_status=0
> begin
> delete from timecard where id=@.id
> fetch c_tc into @.id
> end
>
> What I can see wrong here is that the cursor "c_tc" does not get closed
> and deallocated. Can this cause the problem?
> Thanks
>|||The trigger is poorly written. It does not take multi-row deletes into
consideration. It also uses a cursor which is not needed. ( Not deallocating
the cursor does not seem the issue here, but in general it can cause the
cursor reference to be retained in the memory and the data structures
comprising the cursor are not released. )
Just change the trigger as:
CREATE TRIGGER td_timecardBatch ON [dbo].[TIMECARDBATCH]
FOR DELETE
AS IF @.@.ROWCOUNT = 0 RETURN
DELETE FROM timecard
WHERE EXISTS (
SELECT *
FROM deleted d
WHERE d.batchNumber = timecard.batchNumber )
Anith|||>> does SQL Server log SQL statements for a database?
Not unless you explicitly track them, perhaps using profiler trace or some
proprietary methods.
Anith|||Hi Anith,
Thanks for your reply. Yes this trigger is written poorly and your solution
is excellent.
But our client wants to know why it happened with the current trigger we
have and the only thing i can think of is that the programmer forgets to
close the cursor. I just want to confirm if this can be the cause in any
case?
Thanks
"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:ehZJX%23t6FHA.808@.TK2MSFTNGP09.phx.gbl...
> The trigger is poorly written. It does not take multi-row deletes into
> consideration. It also uses a cursor which is not needed. ( Not
> deallocating the cursor does not seem the issue here, but in general it
> can cause the cursor reference to be retained in the memory and the data
> structures comprising the cursor are not released. )
> Just change the trigger as:
> CREATE TRIGGER td_timecardBatch ON [dbo].[TIMECARDBATCH]
> FOR DELETE
> AS IF @.@.ROWCOUNT = 0 RETURN
> DELETE FROM timecard
> WHERE EXISTS (
> SELECT *
> FROM deleted d
> WHERE d.batchNumber = timecard.batchNumber )
> --
> Anith
>|||>> But our client wants to know why it happened with the current trigger we
Based on the code, it looks like the following line of code might be the
culprit:
select @.batchNumber = batchNumber
from deleted
This will always return only one value and the code within the cursor will
always be executed only once.
Anith|||"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:uVeC9au6FHA.4076@.tk2msftngp13.phx.gbl...
> Based on the code, it looks like the following line of code might be the
> culprit:
>
> select @.batchNumber = batchNumber
> from deleted
> This will always return only one value and the code within the cursor will
> always be executed only once.
> --
> Anith
>
Hi Anith,
The user from the browser can only delete one TIMECARDBATCH record at a
time. So "select @.batchNumber = batchNumber from deleted" is ok as there is
one row to get deleted.
What do you think about forgetting to close the cursor? Can it be a problem
when multiple users are deleting data?
Thanks|||I suspect there is actually an unhandled error due to the FK.
Try deleting a timecardbatch row directly in SQL Query Analyzer and see
if you get an error.
The most solid solution is to change the fk in TIMECARD to cascade
deletes. [and drop that horribly written trigger :) ]
JY wrote:
> we have two tables: TIMECARDBATCH and TIMECARD. TIMECARD has a foreign key
> to TIMECARDBATCH.
> User can only delete one row from TIMECARDBATCH at a time. We have a delet
e
> trigger on TIMECARDBATCH which deletes corresponding records from TIMECARD
.
> In our production environment at one time all records in TIMECARD gets
> deleted but there are still records in TIMECARD. Now we look into the code
> and the programmer whoe code the delete trigger has this code:
> CREATE TRIGGER td_timecardBatch ON [dbo].[TIMECARDBATCH]
> FOR DELETE
> AS
> declare @.batchNumber int
> declare @.id int
> select @.batchNumber = batchNumber
> from deleted
> declare c_tc cursor local for
> select id from timecard where batchNumber=@.batchNumber
> open c_tc
> fetch c_tc into @.id
> while @.@.fetch_status=0
> begin
> delete from timecard where id=@.id
> fetch c_tc into @.id
> end
>
> What I can see wrong here is that the cursor "c_tc" does not get closed an
d
> deallocated. Can this cause the problem?
> Thanks
>

Wednesday, March 7, 2012

Delete user

SQL 2005. and SQL Management studio I have a user 'myuser' in a SQL DB
'mydatabase' under security users. When I try to delete him, I get error
says DB principal owns schema and cant be dropped. I try to remove his role
as DBOwner and it says login name must be specified. (The window above has
his username shown but the loginname field is blank and greyed out/
disabled. I added him to the overall sql DB users / security and it allowed
me to. When I tried to map him to the 'mydatabase' it says 'myuser' already
exists. How can I get rid of this user in the 'mydatabase' DB. I have other
users in the DB that I can delete.
Thanks
bucThe key seems to be that the user owns a schema. See what schema the user ow
ns (schema folder) and
either drop the schema (if empty) or change the owner of the schema to someb
ody else.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
<Buc> wrote in message news:OpjsV6ceGHA.1456@.TK2MSFTNGP04.phx.gbl...
> SQL 2005. and SQL Management studio I have a user 'myuser' in a SQL DB 'my
database' under security
> users. When I try to delete him, I get error says DB principal owns schema
and cant be dropped. I
> try to remove his role as DBOwner and it says login name must be specified
. (The window above has
> his username shown but the loginname field is blank and greyed out/ disabl
ed. I added him to the
> overall sql DB users / security and it allowed me to. When I tried to map
him to the 'mydatabase'
> it says 'myuser' already exists. How can I get rid of this user in the 'my
database' DB. I have
> other users in the DB that I can delete.
> Thanks
> buc
>|||Make sure there are no objects owned by this user. If there are any, you
should change the owner and then you should be able to delete this user from
the database.
Anith|||That worked, he had a entry under the schema folder which had zero objects.
I deleted his entry and it worked. Thanks
"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:e52E89ceGHA.1276@.TK2MSFTNGP03.phx.gbl...
> Make sure there are no objects owned by this user. If there are any, you
> should change the owner and then you should be able to delete this user
> from the database.
> --
> Anith
>

Friday, February 24, 2012

DELETE SQL Query Question - Joining a Table data type and a User Table.

Dear All,
Can anyone help me to run this below query ?
DELETE @._TCTableTemp FROM @._TCTableTemp , Scrip WHERE
@._TCTableTemp.Scrip_Code = Scrip.Scrip_Code
AND Scrip.Inst_Code LIKE 'N%' AND TC_CODE = 'STT'
The error that I am getting is:
Server: Msg 137, Level 15, State 2, Procedure STP_FORMATCCHARGE1, Line 360
Must declare the variable '@._TCTableTemp'.
Here @._TCTableTemp - Is a Table data type and Scrip is a User Table.
Thanks and Regards,
PeriPeri wrote:
> Dear All,
> Can anyone help me to run this below query ?
> DELETE @._TCTableTemp FROM @._TCTableTemp , Scrip WHERE
> @._TCTableTemp.Scrip_Code = Scrip.Scrip_Code
> AND Scrip.Inst_Code LIKE 'N%' AND TC_CODE = 'STT'
> The error that I am getting is:
> Server: Msg 137, Level 15, State 2, Procedure STP_FORMATCCHARGE1, Line 360
> Must declare the variable '@._TCTableTemp'.
> Here @._TCTableTemp - Is a Table data type and Scrip is a User Table.
> Thanks and Regards,
> Peri
Use an alias:
DELETE T
FROM @._TCTableTemp AS T, Scrip
WHERE T.scrip_code = Scrip.scrip_code
AND Scrip.inst_code LIKE 'N%'
AND tc_code = 'STT' ;
The table variable is only a temporary structure so in many cases I
wouldn't bother to delete from it. Just ignore the rows you planned to
delete.
David Portas
SQL Server MVP
--

Friday, February 17, 2012

Delete problem

Dear all,

I have an asp.net webform which will provide delete function. If there are foreign key constraint and the user click the delete button, i would like the user to get response (Eg You must delete other data first......or something like this)

1. Any good idea?

2. One way i search from this form is like this, it raise error in db side(stored proc)

IF EXISTS (SELECT id FROM A WHERE ID = @.ID)
BEGIN
RAISERROR (''VersionA.)
RETURN
END

IF EXISTS (SELECT id FROM A WHERE ID = @.ID)
BEGIN
RAISERROR (''VersionB.)
RETURN
END

How can i get the raiseerror and identiy the veriosn of error in asp.net page?

Thanks in advance!!!!

Return a value to indicate that child records exists. Raising an error is not good idea due to performance.

|||This should already be taken care of from the sql side as long as youhave enforce constraits on for deleting and crud operations. Allyou have to do is catch the SQL errors and handle them from your aspxwhich isnt hard. Search handling sql errors from the codebehind. All you would have to do is handle the event and showjavascript to the user that you need to delete things.

delete open connection on database

Hi,
Does anyone know how i can disconnect an user who has a open session
on a database?
The reason is i'd like to restore a database in a job but the job
always fails because of users which have an open session.
Is there a stored procedure which i can use before i use mit "restore
database"-statement?
Thx 4 Help,
Look up the kill command in Books Online, though using this frequently will
make you a very unpopular person.
Peter Yeoh
http://www.yohz.com
Need smaller SQL2K backup files? Try MiniSQLBackup
"holzi" <holzi@.bluemail.ch> wrote in message
news:ded7aa56.0406282334.52797e51@.posting.google.c om...
> Hi,
> Does anyone know how i can disconnect an user who has a open session
> on a database?
> The reason is i'd like to restore a database in a job but the job
> always fails because of users which have an open session.
> Is there a stored procedure which i can use before i use mit "restore
> database"-statement?
> Thx 4 Help,
|||Look up the kill command in Books Online, though using this frequently will
make you a very unpopular person.
Peter Yeoh
http://www.yohz.com
Need smaller SQL2K backup files? Try MiniSQLBackup
"holzi" <holzi@.bluemail.ch> wrote in message
news:ded7aa56.0406282334.52797e51@.posting.google.c om...
> Hi,
> Does anyone know how i can disconnect an user who has a open session
> on a database?
> The reason is i'd like to restore a database in a job but the job
> always fails because of users which have an open session.
> Is there a stored procedure which i can use before i use mit "restore
> database"-statement?
> Thx 4 Help,
|||holzi,
Why not put it into single user mode during the restore?
i.e.
ALTER DATABASE mydb SET SINGLE_USER WITH ROLLBACK IMMEDIATE
go
RESTORE mydb...
go
ALTER DATABASE mydb SET MULTI_USER
go
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
holzi wrote:
> Hi,
> Does anyone know how i can disconnect an user who has a open session
> on a database?
> The reason is i'd like to restore a database in a job but the job
> always fails because of users which have an open session.
> Is there a stored procedure which i can use before i use mit "restore
> database"-statement?
> Thx 4 Help,
|||holzi,
Why not put it into single user mode during the restore?
i.e.
ALTER DATABASE mydb SET SINGLE_USER WITH ROLLBACK IMMEDIATE
go
RESTORE mydb...
go
ALTER DATABASE mydb SET MULTI_USER
go
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
holzi wrote:
> Hi,
> Does anyone know how i can disconnect an user who has a open session
> on a database?
> The reason is i'd like to restore a database in a job but the job
> always fails because of users which have an open session.
> Is there a stored procedure which i can use before i use mit "restore
> database"-statement?
> Thx 4 Help,

delete open connection on database

Hi,
Does anyone know how i can disconnect an user who has a open session
on a database?
The reason is i'd like to restore a database in a job but the job
always fails because of users which have an open session.
Is there a stored procedure which i can use before i use mit "restore
database"-statement?
Thx 4 Help,Look up the kill command in Books Online, though using this frequently will
make you a very unpopular person.
Peter Yeoh
http://www.yohz.com
Need smaller SQL2K backup files? Try MiniSQLBackup
"holzi" <holzi@.bluemail.ch> wrote in message
news:ded7aa56.0406282334.52797e51@.posting.google.com...
> Hi,
> Does anyone know how i can disconnect an user who has a open session
> on a database?
> The reason is i'd like to restore a database in a job but the job
> always fails because of users which have an open session.
> Is there a stored procedure which i can use before i use mit "restore
> database"-statement?
> Thx 4 Help,|||holzi,
Why not put it into single user mode during the restore?
i.e.
ALTER DATABASE mydb SET SINGLE_USER WITH ROLLBACK IMMEDIATE
go
RESTORE mydb...
go
ALTER DATABASE mydb SET MULTI_USER
go
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
holzi wrote:
> Hi,
> Does anyone know how i can disconnect an user who has a open session
> on a database?
> The reason is i'd like to restore a database in a job but the job
> always fails because of users which have an open session.
> Is there a stored procedure which i can use before i use mit "restore
> database"-statement?
> Thx 4 Help,

delete open connection on database

Hi,
Does anyone know how i can disconnect an user who has a open session
on a database?
The reason is i'd like to restore a database in a job but the job
always fails because of users which have an open session.
Is there a stored procedure which i can use before i use mit "restore
database"-statement?
Thx 4 Help,Look up the kill command in Books Online, though using this frequently will
make you a very unpopular person.
Peter Yeoh
http://www.yohz.com
Need smaller SQL2K backup files? Try MiniSQLBackup
"holzi" <holzi@.bluemail.ch> wrote in message
news:ded7aa56.0406282334.52797e51@.posting.google.com...
> Hi,
> Does anyone know how i can disconnect an user who has a open session
> on a database?
> The reason is i'd like to restore a database in a job but the job
> always fails because of users which have an open session.
> Is there a stored procedure which i can use before i use mit "restore
> database"-statement?
> Thx 4 Help,|||holzi,
Why not put it into single user mode during the restore?
i.e.
ALTER DATABASE mydb SET SINGLE_USER WITH ROLLBACK IMMEDIATE
go
RESTORE mydb...
go
ALTER DATABASE mydb SET MULTI_USER
go
--
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
holzi wrote:
> Hi,
> Does anyone know how i can disconnect an user who has a open session
> on a database?
> The reason is i'd like to restore a database in a job but the job
> always fails because of users which have an open session.
> Is there a stored procedure which i can use before i use mit "restore
> database"-statement?
> Thx 4 Help,