we have updated our database from sqlanywhere 11 to 16. Unfortunately after the update all views, procedures, functions in sybase central have quoted column names.
f.e.
SELECT "customer_id", "customer_name", "customer_nr" as "cNr" from "customer"
Is there an option to disable these quotes? I tried with "SET quoted_identifier Off;" but this didn't work.
Related
I'm using Flyway for database versioning for my SQL Server 2014 database. I have 800+ stored procedures.
In the initial migration, for around 394 stored procedures, QUOTED_IDENTIFIER is set to OFF. But when I migrate it separately having it as the only stored procedure, then the QUOTED_IDENTIFIER is set to ON.
Always the default value of QUOTED_IDENTIFIER is ON.
I tried setting the QUOTED_IDENTIFIER value (QUOTED_IDENTIFIER=ON) in JTDS connection string but no use.
Is there any workaround for this ?
Quoted_Identifier and Ansi_Nulls and a couple of other pesky attributes are "sticky". The current state of Quoted_Identifier (in the session) controls what "sticks to" a newly introduced procedure or function as they're created. Some tools are better, some worse about scripting out the setting to be inserted just prior to the create procedure or create function statements. You may or may not see the generated statement when you look at the source in the db...depends on the tooling your using.
These attributes are captured into sys.sql_modules, which has the source text and the state of uses_ansi_nulls and uses_quoted_identifier and the other sticky attributes. You can use these flags to know how to script out any statement ahead of the procedure and/or function.
The MS SQL Server version in question is 8.
In the context of database other than master if I call stored procedures from master database, for some of them I must append master.. prefix (otherwise I get Could not find stored procedure 'procname' error), for some of them I don't.
For example, I can call --
EXEC sp_addlogin 'user' 'pass';
-- and it works, but --
EXEC xp_cmdshell 'command';
-- doesn't. I have to prepend master.. for it to work --
EXEC master..xp_cmdshell 'command';
I may be wrong here but I observed that one have to add master.. to only those stored procedures that start with xp_ (as opposed to sp_).
Why do I have to call some of them with master.. prepended while some of them can be called without?
Procedures in the master database whose name begin with sp_ can be called in any other user database without having to add the master.. prefix. Since procedures beginning with xp_ don't follow that rule, you still need to add the master.. prefix when calling them.
See this link for more information.
The xp_ stands for extended stored procedures. They are stored in the master database.
The sp_ are for "special". They are Microsoft-shipped procedures that exist in every database. You can see them in the Object Explorer by going (Database Name) > Programmability > Stored Procedures > System Stored Procedures. Because they are in the same database as your query, you don't need to add master..
As a side note, it would be wise to NOT name your own stored procedures with sp_.... See explanation here.
Can anybody help me with this?
I prefer if I don't have to explicitly list the table names.
Saw this before...
exec sp_MSforeachtable "DROP TABLE ? PRINT '? to be dropped' "
Source - http://sqlserver-qa.net/blogs/t-sql/archive/2008/05/20/4266.aspx
How about dropping the database? DROP DATABASE <database name>
Of course, that's rough on stored procedures, triggers, etc. But if the purpose is to eliminate all the tables in order to recreate them, it makes sense that you'd recreate all the other associated components as well, such as indexes.
How do I get away with hardcoding the database name in referencing a table within a stored procedure. For example there are two databases db1 and db2. I am writing a stored procedure in db2 which references two tables, one from db1 and another from db2. Both are on the same sybase server.
If I understand your question correctly, on the one hand, in your stored procedure you can refer to the table in the same database directly by name
SELECT ...
FROM table_in_db2
You can refer to a table in database db1 by prefixing the database name and an empty owner:
SELECT ...
FROM db1..table_in_db1
On the other hand, if you want to avoid hard-coding database names in the procedure you might create a view in database db2 that references the db1 table:
CREATE VIEW view_in_db2
AS
SELECT *
FROM db1..table_in_db1
and use that in the procedure:
SELECT ...
FROM view_in_db2
You need to keep the code portable, involve 2 databases, but avoid referencing databases by name. Then you can create proxy tables (or proxy views, if such views exist in 12.5). Refer to proxy tables as to local objects.
This will work, but will require some extra care, every time you move/change databases. But anyway the separation of concerns you are after can be achieved.
I have a case sensitive SERVER (SQL_Latin1_General_CP1_CS_AS) but the Database is Case insensitive (SQL_Latin1_General_CP1_CI_AS).
If I try to create the following stored procedure on the database, I get the error "Must declare the scalar variable "#test"."
CREATE PROCEDURE [dbo].[sp_Test] (#TEST int) as
begin
SELECT #test
end
GO
But as I stated the database itself is not case sensitive. Im assuming this is documented somewhere that stored procedures follow the sensitivity of the server but I cannot find a reference anywhere. Can anyone point me to where I would find some docs about this? (Yes I tried google, but im not finding anything)
You are right. Database collation does not control variables name case sensitivity - server collation does.
Any other object name (e.g. table, view, column) follows database collation rules. In your situation, that means case insensitive, since your database is CI (case insensitive).
From the SQL Server Books Online:
COLLATE (Transact-SQL)
The collation of an identifier depends on the level at which it is defined.
Identifiers of instance-level objects, such as logins and database names, are assigned the default collation of the instance.
Identifiers of objects within a database, such as tables, views, and column names, are assigned the default collation of the database.
For example, two tables with names different only in case may be created in a database with case-sensitive collation, but may not be created in a database with case-insensitive collation. For more information, see Database Identifiers.
The identifiers for variables, GOTO labels, temporary stored procedures, and temporary tables are in the default collation of the server instance.
Variables, GOTO labels, temporary stored procedures, and temporary tables can be created when the connection context is associated with one database, and then referenced when the context has been switched to another database.
You can check your server collation using:
SELECT SERVERPROPERTY('collation');
SQL_Latin1_General_CP1_CI_AS
(1 row(s) affected)
See also
MSDN forums: Why are my SP's throwing a case error when pushing to a db using BIN collation?
Case sensitive variables in SQL Server
Case sensitive variable names in SQL Server?