2

I've got some SQL queries like this

select user_id from 
table_user where lower(email_address)=? and password=?

The schema for the application was recently updated but I don't really want to update every SQL query in the application. Is there a way to specify the current Schema from the JBOSS connection end?

Old connection: jdbc:sqlserver://myserver:1433;DatabaseName=db Tried: jdbc:sqlserver://myserver:1433;DatabaseName=db;currentSchema=abc

I tried using currentSchema but that didn't help, I get a missing object exception when I run the queries (since I assume these are looking under dbo). Is there any way around updating the queries since I know that all the queries will run on schema abc?

4

1 回答 1

2

这些是Microsoft JDBC 4.0 驱动程序的可用连接属性。我currentSchema在这个列表中没有看到,也没有看到任何允许您在连接字符串中指定特定模式的驱动程序。

由于您不想使用架构更新 SQL,您可以在默认 (dbo) 架构中为每个对象创建同义词。例如:

USE tempdb;
GO
-- create test schema
CREATE SCHEMA test AUTHORIZATION dbo;
GO

-- create table in test schema
CREATE TABLE test.tablename (columnname int null);

-- select from tablename in default schema will fail
SELECT * FROM tablename;
GO

-- create synonym mapping test.tablename to dbo.tablename
CREATE SYNONYM [dbo].[tablename] FOR [server].[tempdb].[test].[tablename]

-- -- select from tablename synonym will succeed
SELECT * FROM tablename;

-- cleanup
DROP SYNONYM [dbo].[tablename];
DROP TABLE [test].[tablename];
DROP SCHEMA [test];

您可以使用以下代码CREATE SYNONYM为用户对象生成语句。如果使用它,则需要在执行前更新变量值并查看语句。没有明示或暗示的保证:)

-- generate create synonym statements for user objects
DECLARE @FromSchema SYSNAME = 'abc',
        @ToSchema SYSNAME = 'dbo',
        @ServerName SYSNAME = 'server',
        @DatabaseName SYSNAME = 'database';

SELECT  'CREATE SYNONYM ' + QUOTENAME(@ToSchema) + '.' + QUOTENAME(name) +
        ' FOR ' + QUOTENAME(@ServerName) + '.' + QUOTENAME(@DatabaseName) +
        '.' + QUOTENAME(@FromSchema) + '.' + QUOTENAME(name) + ';'
FROM    sys.objects
WHERE   is_ms_shipped = 0;
于 2013-07-23T21:14:49.103 回答