I am running Sybase Adaptive Server Enterprise 15.7, please could anyone tell me how to write a procedure that iterates through each table within the database and truncates the data in each table.
Thanks
:-)
There's two ways:
(i) write it yourself by cycling over sysobjects and constructing a truncate table command for every table found, and then executing it with exec(#cmd).
(ii) download my stored procs from http://www.sypron.nl/new_ssp_dwn.html, install them and then run:
sp_rv_findobject 'db=your_db_name', type=U', 'exec=immediate', 'execarg=truncate table OW.NM'
Related
I am a new man for PostgresQL; working in DBEaver. I have created a procedure that modifies, among others, a temp table. I would like to print out the table for testing purposes: to see what is in the table now.
In T-SQL I could just execute “select * from MyTestTable”; and this was output to SQL Studio Grid tab. This did not break the procedure.
Now on Postgres I am using DBeaver and get errors when I try to use the same approach.
A question to experienced PostgresQL: how you cope with that? Is there any way to “peek my nose” into middle of a proc and see – what data are at given moment in table. If no - how to debug large and complicated procedures without ability to look at produced data Grid?
I have a data anonymization process that takes a production copy of a database and turns it into an anonymized copy by UPDATE-ing some columns.
Some of the tables contain several million rows so instead of UPDATE-ing the columns, which is very log intensive, I went down the way of
SELECT
Id,
CAST('Redacted' AS NVARCHAR(255)) [ColumnRequiringAnonymization]
INTO MyTable_New
FROM MyTable
EXEC sp_rename MyTable, MyTable_old
EXEC sp_rename MyTable_new, MyTable
DROP TABLE MyTable_old
The problem with this approach is that the "new" table no longer has any of the keys, indices and other dependent objects. I have figured out the keys and indices using SPs to generate the DROP and CREATE scripts. The SPs are based on manually written SQL as can be seen e.g. in this answer.
The next problem is that we have a schemabound view on top of this table, which has indices and a full-text index on its own. The number of SPs to generate scripts is growing and I am sure there will be mistakes.
Is there a way to completely script a table/view by using SQL commands only? ie. just like SSMS does when you click "Script table as - CREATE to" but within a stored procedure?
Right-click on the database, select Tasks; there is Generate Scripts there. Just follow prompts or Google for additional information.
Is there a stored procedure or some SQL that I could run that would display the SQL for creating a table from an existing table? Like sp_helptext to display the contents of a function or stored procedure. Basically, is there a way to do the Script Table As->CREATE TO method?
The answer is no. If you start profiler, and run [scirpt table]>[create to] in SSMS, then you'll see a series of sp_executesql being ran on sys.* tables. This means that no CREATE TABLE commands are stored anywhere in SQL server, and SSMS assemblies CREATE statements for a table from a lot of different sources.
On the other hand, if run [script view]>[create to], you'll see a simple query from sys.all_objects, sys.sql_modules, sys.system_sql_modules, where definitions are stored.
I have a stored procedure that use temp table and explicitly drops it when done.
What happed when same procedure is run at same time by 2 different sessions? Sessions are run as single user (webapp).
Will one session interfere with temp table, and data inside it, of other session?
I'm using Postgres 9.0.
In postgresql temporary tables are unique for each session, so no problem.
If I am not totally wrong, the result would be one temp table per query in your procedure which generates the temp table, so the two temp tables would not disturb eachother.
H all,
Fist of all, thanks for reading this.
My Question is, how can I select a bunch of data from ANOTHER database and insert to my own database with same coloum name and field?
I just can think of is using select from DB1 and then insert into DB2.
I plan to written this process inside a stored procedures.
Is there a better way to do so?
Development enviroment :Sql server 2008 and VS2010(using .net C# to excecute Stored prod)
Thank you, Appreciate its lot.
And Please don't hesitate to voice out my error or mistake.I wish to learn from mistake
LiangCk
You can do the following if its the same database server
INSERT INTO [DB].[UserName].[TableName]
Select * from [DB2].[UserName].[TableName]
[DB] is the name of database 1
[DB2] is the name of database 2
[UserName] is your sql server username (dbo,....)
[TableName] is, off course, your table
If you have different sql servers you can connect both servers
using linked server.
http://msdn.microsoft.com/en-us/library/ff772782.aspx