Extract SP and DDL Scripts in sybase Server - sql

How do I extract DDL, Stored Procedures (SP) and other database scripts from Sybase ASE.

Look into the ddlgen utility included with sybase, ususally found in the Sybase Central directory, or the $SYBASE/ASEP/bin. This should be able to generate scripts to create all of the database objects including user-defined datatypes (UDD), indexes, stored procedures, etc.

You can use sp_helptext to show the DDL.

Related

Can I write a SQL script to dynamically create all stored procedures and views from one database to another?

Can I write a SQL script to dynamically create all stored procedures and views from one database to another?:
I have 2 instances of MyDatabase
1 MyDatabase instance on ServerX
1 MyDatabase instance on ServerY
I'd like to write a SQL script which does the following:
Drop all stored procedures and views on ServerY
Generate CREATE statements for all stored procedures and views on ServerX
Execute those CREATE statements on ServerY, so all stored procedures and views on ServerY match those on ServerX
I'm sure this can be done but can anyone here describe a way to go about doing this?
There is an easier way to do this - SQL Server can script the creation of the objects for you into a single *.sql file. You then just run that script on the other server. You can even have it include the data from the existing database. For a detailed walk through, see: https://dzone.com/articles/generate-database-scripts-with-data-in-sql-server
Why? SQL Server has all this built in for you
Once all your SP and views are on Server.. Right click on your database and click Tasks > Generate scripts. SQL Server Management Studio is able to generate the CREATE scripts for you, which can be done on SP, views and more.
Then you simple copy this script and execute it on ServerX server/database.
BUT if you need it to be automated you should use powershell to simulate this task. Doing this in a SQL script isn't the best solution.
Create a link from the server X to the server Y and select, assume server x for the primary.

How can I restore my Stored Procedures again into dababase at once?

I had done generate script and take out my 100+ stored procedures into a separate 100 files like we take backup now i made changes into those files.
but now i need to alter all those original stored procedures
from database.
i mean to say now all those 100 files i need to again load into database.
How can I do this like I had generate script to take out those sp's from database
is there any way to restore all new sp's into same database at once?
please help.
If you wish to take out the scripts of stored procedures, you can take out them using "Generate Script" you shall get CREATE scripts. Once you change the scripts' body, you can rerun the scripts on the DB. But before that you can either drop the SP in the database or change the CREATE statement to ALTER in the script file.
One more way is to generate ALTER script when you take out the script. But this works with one proc at a time. You can right-click the SP and select "Script Stored Procedure as -> ALTER to -> File..." from the context menu.
You can backup your database stored procedures into a file, and restore them to your new database server using Backup and Restore tool.
ApexSQL Build does exactly what you need. It executes SQL scripts against new or existing databases, and you can specify to drop the object first, so there will be no conflicts when executing CREATE PROCEDURE statements
You can create T-SQL, C# or exe
Please use this script for execute in your database...
Select
Convert(varchar(Max), Substring([RowLog Contents 0], 33, LEN([RowLog Contents 0]))) as [Script]
from
fn_dblog(NULL, NULL)
You will see script stored procedure are dropped.
Good luck :)

Possible to create a SQL stored procedure for use for all databases

I have a stored procedure, in one database within my SQL server, that sets permissions to all stored procedures at once for that particulat database. Is there a way to create this stored procedure in a way were I can call it easily from any database within the SQL server and if so how do I go about doing such a thing
While the best solution to this specific question of granting execute to all procedures is the one provided by marc_s, the actual question was is there a way to create a single stored procedure and make it available to all databases.
The way to do this is documented at https://nickstips.wordpress.com/2010/10/18/sql-making-a-stored-procedure-available-to-all-databases/:
Create the stored procedure in the master database.
It must be named to start with sp_, e.g. sp_MyCustomProcedure
Execute sys.sp_MS_marksystemobject passing the name of the procedure, e.g. EXEC sys.sp_MS_marksystemobject sp_MyCustomProcedure
Here is a simple example which just selects the name of the current database:
use master
go
create procedure dbo.sp_SelectCurrentDatabaseName as begin
select db_name()
end
go
execute sys.sp_MS_marksystemobject sp_SelectCurrentDatabaseName
go
Calling exec dbo.sp_SelectCurrentDatabaseName on any database will then work.
To mark the procedure as not a system object, there a some nasty hacks suggested at https://social.msdn.microsoft.com/Forums/sqlserver/en-US/793d0add-6fd9-43ea-88aa-c0b3b89b8d70/how-do-i-undo-spmsmarksystemobject?forum=sqltools but it is safest and easiest to just drop and re-create the procedure.
Caveat
Of course creating system procedures like this is breaking the common rule of not naming your own procedures as sp_xxx, due to the possibility of them conflicting with built-in procedures in future versions of SQL Server. Therefore this should be done with care and not just create a load of randomly named procedures which you find useful.
A common simple way to avoid this is to add your own company/personal prefix to the procedure which Microsoft is unlikely to use, e.g. sp_MyCompany_MyCustomProcedure.
I have a stored procedure, in one database within my SQL server, that
sets permissions to all stored procedures at once for that particular
database.
You could archive the same result much easier:
create a new role, e.g. db_executor
CREATE ROLE db_executor
grant that role execute permissions without specifying any objects:
GRANT EXECUTE TO db_executor
This role now has execute permissions on all stored procedures and functions - and it will even get the same permissions for any future stored procedure that you add to your database!
Just assign this role to the users you need and you're done....
Have you tried a 3 or 4 part name?
InstanceName.DatabaseName.dbo.usp_Name
That procedure could in turn reference objects in other databases using the same conventions. So you could parameterize the name of the database to be operated on and use dynamic SQL to generate 4 part names to reference objects such as system tables.

Does naming function in MSSQL like fn_myFuction take additional performace

Someone here mentioned that We should avoid naming stored procedures in MS SQL server like sp_XXX
Because that taking addition time to SQL server while check does exist system sotred procedure named like that. Because all system stored procs are starting with sp_.
Now I wondering is that a case with Functions in MSSQL, Does naming functions like fn_ take additional time to SQL while looking for system functions ?
No, I don't think so.
Found the following thread:
http://bytes.com/topic/sql-server/answers/78872-udf-starting-fn_
No. To call a system-supplied [User Defined Function], you
need to use ::, so that is what SQL
Server looks for. All system-supplied
UDFs are table functions, as scalar
system functions are not UDFs at all.
Hope this helps
For functions it does not matter, however it is recommended to NOT use a prefix of sp_ for stored procedures as it defines a system stored procedure and may cause an extra lookup in the MASTER database.
As sp_ prefix is reserved for system
stored procedure and any stored
procedure which has sp_ prefix will
cause an extra lookup in MASTER
database. There is another point to
note that if a stored procedure uses
same name, in user database as system
stored procedure in master database,
the stored procedure in user database
will never get executed as SQL Server
will always look first in master
database and will execute that one
rather one in user database.
http://furrukhbaig.wordpress.com/2007/08/22/stored-procedures-factssheet/
http://msdn.microsoft.com/en-us/library/dd172115.aspx

Testing Stored Procedures with MySQL

What is the best way to test MySQL stored procedures? How do you test stored procedures with output parameters in the MySql GUI?
My standard method is to create SQL scripts that create the test data, call the stored procedure, and automatically verify the post-conditions of the stored procedure. You could use the MySQL Query Browser to write/run the scripts.
HTH -
Chris