delete all records in SqlServer table

1) how to delete all record in sql table?
2)  and how to assign a var in sql instead to use a fixed name of table (in effect the var is dinamilcly fille from other part fo code)
 
This is my conn and code in a VB 6.0:

Sub ADO_ACCESS_To_SQLServer()

'DA ACCESS A SQL

Set rsSql = New ADODB.Recordset
Set CnSql = New ADODB.Connection

ServerName = "CL151F4500\SQLEXPRESS"
DatabaseName = "MASTER"
TableName = "SOSPESI"
UserID = ""
Password = ""

CnSql.Open "Driver={SQL Server};Server=" & ServerName & ";Database=" & DatabaseName & _
";Uid=" & UserID & ";Pwd=" & Password & ";"

rsSql.Open TableName, CnSql, adOpenKeyset, adLockOptimistic

SQL = "SELECT * FROM SOSPESI"
CnSql.Execute (SQL)
SQL = "truncate table SOSPESI"
CnSql.Execute (SQL)
........
sal21Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DimitrisSenior Solution ArchitectCommented:
Try this
SQL = "SELECT * FROM " & TableName
CnSql.Execute (SQL)
SQL = "truncate table " & TableName -- Try not to use truncate because sometimes locks the entire database Use delete statement instead "DELETE FROM " & TableName
CnSql.Execute (SQL)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you mean:
Set CnSql = New ADODB.Connection 
ServerName = "CL151F4500\SQLEXPRESS"
DatabaseName = "MASTER"
TableName = "SOSPESI"
UserID = ""
Password = "" 
CnSql.Open "Driver={SQL Server};Server=" & ServerName & ";Database=" & DatabaseName & _
";Uid=" & UserID & ";Pwd=" & Password & ";" 
SQL = "truncate table [" & TableName & "]"
CnSql.Execute SQL

Open in new window

0
Patrick MatthewsCommented:
sal21 said:
>>1) how to delete all record in sql table?

To delete:

DELETE FROM [TableName]

To truncate:

TRUNCATE TABLE [TableName]

Truncating is much, much faster, but the deletions are not logged, so you cannot roll back the transaction.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
>Try not to use truncate because sometimes locks the entire database
never heard of that?

however, TRUNCATE might not be possible, ie when you have foreign keys pointing to your table, for example.
0
Patrick MatthewsCommented:
Looks like I need more coffee :)
0
sal21Author Commented:
ok i testd...

But the aline:

rsSql.Open TableName, CnSql, adOpenKeyset, adLockOptimistic

is need in this contest?
0
sal21Author Commented:
ahhhh....
and how to order by desc all record based 2 filed, is correct the line:

    SQL = Empty
    SQL = "SELECT PROVA12, PROVA04 FROM SOSPESI ORDER BY PROVA12 DESC"
    DB2.Execute (SQL)
0
DimitrisSenior Solution ArchitectCommented:
I think that this question is not relevant to your initial question and that you have to put an other post.

But
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
the SELECT is not relevant, you don't need to query the data you want to delete (especially as you want to delete all the rows)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.