I am running into a problem with synonym for Oracle.
One of the database package is giving me this error: looping chain of synonym error, on the line where UTL_MAIL.SEND is called. After reading several stack posts, I came up with the following query:
SELECT S.OWNER as SYN_OWNER, S.SYNONYM_NAME as SYN_NAME,
S.TABLE_OWNER as OBJ_OWNER, S.TABLE_NAME as OBJ_NAME,
O.STATUS as OBJ_STATUS
FROM DBA_SYNONYMS S
LEFT JOIN DBA_OBJECTS O ON S.TABLE_OWNER = O.OWNER AND S.TABLE_NAME = O.OBJECT_NAME
WHERE (O.OWNER is null
OR O.STATUS != 'VALID') and S.SYNONYM_NAME like '%UTL%'
I found that the UTL_MAIL synonym has a status of null, which indicates a missing synonym. However, there are many other public/private synonyms that are either invalid or missing(500+ of them). It seems like something didn't get set up correctly.
When I ran a simpler version of the query: SELECT OWNER, SYNONYM_NAME, TABLE_OWNER, TABLE_NAME FROM DBA_SYNONYMS
I have the following result for UTL_MAIL:
Owner SYNONYM_NAME TABLE_OWNER TABLE_NAME
Public UTL_MAIL SYS UTL_MAIL
What I have tried so far :
I logon as SYSTEM user and ran CREATE OR REPLACE NONEDITIONABLE PUBLIC SYNONYM "UTL_MAIL" FOR "SYS"."UTL_MAIL";
The output says the synonym is created. However, when I ran the above query, its status still showing up as null.
What I need help with:
How to create UTL_MAIL synonym properly?
See if you can find a looping synonym chain using a hierarchical query:
select owner, synonym_name, connect_by_iscycle CYCLE
from dba_synonyms
where connect_by_iscycle > 0
connect by nocycle prior table_name = synonym_name
and prior table_owner = owner
Related
I am using Azure Managed Instance for some migration tasks. I have multiple databases in that all are working fine.
But when I try to open the properties for a database named GCDCalculation, I am getting the following error:-
And the whole error is:-
Cannot show requested dialog.
===================================
Cannot show requested dialog. (SqlMgmt)
------------------------------
Program Location:
at Microsoft.SqlServer.Management.SqlMgmt.DefaultLaunchFormHostedControlAllocator.AllocateDialog(XmlDocument initializationXml, IServiceProvider dialogServiceProvider, CDataContainer dc)
at Microsoft.SqlServer.Management.SqlMgmt.DefaultLaunchFormHostedControlAllocator.Microsoft.SqlServer.Management.SqlMgmt.ILaunchFormHostedControlAllocator.CreateDialog(XmlDocument initializationXml, IServiceProvider dialogServiceProvider)
at Microsoft.SqlServer.Management.SqlMgmt.LaunchForm.InitializeForm(XmlDocument doc, IServiceProvider provider, ISqlControlCollection control)
at Microsoft.SqlServer.Management.SqlMgmt.LaunchForm..ctor(XmlDocument doc, IServiceProvider provider)
at Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.ToolMenuItemHelper.OnCreateAndShowForm(IServiceProvider sp, XmlDocument doc)
at Microsoft.SqlServer.Management.SqlMgmt.RunningFormsTable.RunningFormsTableImpl.ThreadStarter.StartThread()
===================================
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. (.Net SqlClient Data Provider)
------------------------------
For help, click: https://learn.microsoft.com/sql/relational-databases/errors-events/mssqlserver-512-database-engine-error
------------------------------
Server Name: my-project-database.database.windows.net, 3342
Error Number: 512
Severity: 16
State: 1
Line Number: 7
------------------------------
Program Location:
at Microsoft.SqlServer.Management.SqlManagerUI.DBPropGeneralData.InitProp()
at Microsoft.SqlServer.Management.SqlManagerUI.DBPropGeneralData..ctor(CDataContainer context)
at Microsoft.SqlServer.Management.SqlManagerUI.DBPropGeneral..ctor(CDataContainer dataContainer, DatabasePrototype prototype)
at Microsoft.SqlServer.Management.SqlManagerUI.DBPropSheet.Init(CDataContainer dataContainer)
at Microsoft.SqlServer.Management.SqlManagerUI.DBPropSheet..ctor(CDataContainer context)
All other Databases are working fine.
Added to this, I want to know that, is there specific query is triggered in the backend when we try to open the properties tab (Or any other tab).
Finally, one I can answer. Kollira is on the right track with this, the reason you get the error is that backupset contains records linking more than one database to the same ID. This happens because the DB was probably brought into SQL MI via the migration tool which creates the backupset with a GUID instead of the database name. When you do your own copy-only backup to URL, it creates a new backupset with the real name. This is not an optimised query to prove it but here goes.
select db_id(database_name),database_name, [type], max(backup_finish_date) as latest from msdb..backupset where ([type] = 'D' or [type] = 'L' or [type]='I') and db_id(database_name) = (select database_id from sys.databases where name = 'Your_Database') group by database_name, [type]
Then we can see there is more than one DB listed, you can probably see the wrong one as it was created just when you backed up. So with this list of probably 2 DBs
Select * from msdb..backupset where database_name in('DB1', 'DB2')
We can see conflicting backupsets. In my case the one with the real name was actually the new one that I wanted to remove, the other was a GUID I assume from my migration. So I just deleted the backupset as there were no backups associated. So simply:
Delete from msdb.dbo.backupset where database_name = 'TheDatabaseName'
Boom, now properties rendered, and the original backup chain was still correct.
I am not a SQL pro, so use at your own risk. Also, since this is no longer a Managed instance unique problem, maybe a SQL pro can advise a better solution.
The problem is with backups. Run this query and update the database name to the one which is latest.
create table #tempbackup (database_name nvarchar(128), [type] char(1), backup_finish_date datetime)
insert into #tempbackup
select database_name, [type], max(backup_finish_date) from msdb..backupset where [type] = 'D' or [type] = 'L' or [type]='I' group by database_name, [type]
SELECT
(select backup_finish_date from #tempbackup where type = 'D' and db_id(database_name) = dtb.database_id)
AS [LastBackupDate]
FROM
master.sys.databases AS dtb
WHERE
(dtb.name='Your Database Name')
drop table #tempbackup
I have a Snowflake table which gets its data (via COPY INTO) from an S3 bucket. When I tried to run the below statement to check the load status, it didn't give any result.
SELECT * FROM TABLE(INFORMATION_SCHEMA.COPY_HISTORY(TABLE_NAME=>'HourlyTransactionStaging', START_TIME=> DATEADD(DAY, -14, CURRENT_TIMESTAMP())));
Instead, I got this error
Table DBNAME.STAGING.HOURLYTRANSACTIONSTAGING did not exist or was purged.
However, when I tried to run this, it ran and gave me the results as well.
select * from information_schema.load_history
Where
Schema_name = 'STAGING'
AND TABLE_NAME = 'HOURLYTRANSACTIONSTAGING';
I figured out what the issue was. Apparently, TABLE_NAME parameter in the COPY_HISTORY function is case sensitive and I was providing the table name as per the conventions.
HourlyTransactionStaging --> HOURLYTRANSACTIONSTAGING
Glad you figured it out. Also you need to make sure that you're on the correct database / schema before running the query as below:
use schema your_db.schema;
select *
from table(information_schema.copy_history(table_name=>'table_name', start_time=> dateadd(hours, -1, current_timestamp())));
When I execute a stroed procedure it works. But specifically when I run the select statement I get the following error
Synonym 'syn.Syn_NEO_DB_tGradeAliases' refers to an invalid object.
SELECT
aa.CompanyId [LegCompanyId],
aa.ProductId AS [LegGradeId],
aa.GradeAliasId [LegGradeAliasId],
aa.ProductName AS [LobGradeText],
aa.[Alias] [GradeAlias],
aa.PhraseKey [PhraseKey],
GETUTCDATE() AS 'TimeStamp'
FROM syn.Syn_AAA aa
I haven't done any database change/permission change.
How can I overcome this?
I ran following query and it shows the base_object_name correctly linked to my table.
SELECT * FROM sys.synonyms WHERE name = 'Syn_AAA'
I overcame by replacing synonym with [LINKED_SERVER].[DB_NAME].[SCHEMA_NAME].[OBJECT_NAME].
There is always a need to find out details, either intentionally Or mistakenly someone executed DROP/DELETE command on any of following SQL Server database objects.
DROPPED - Table from your database
DROPPED - Stored Procedure from your database
DELETED - Rows from your database table
Q. Is there TSQL available to find db user who performed DELETE/DROP?
Q. What kind of permissions are needed for user to find out these details?
Did you check this ?
Right click on database.
Go to as shown in image :
Solution 2 :
This query gives alot of useful information for a database(apply filter as required) :
DECLARE #filename VARCHAR(255)
SELECT #FileName = SUBSTRING(path, 0, LEN(path)-CHARINDEX('\', REVERSE(path))+1) + '\Log.trc'
FROM sys.traces
WHERE is_default = 1;
SELECT gt.HostName,
gt.ApplicationName,
gt.NTUserName,
gt.NTDomainName,
gt.LoginName,
--gt.SPID,
-- gt.EventClass,
te.Name AS EventName,
--gt.EventSubClass,
-- gt.TEXTData,
gt.StartTime,
gt.EndTime,
gt.ObjectName,
gt.DatabaseName,
gt.FileName,
gt.IsSystem
FROM [fn_trace_gettable](#filename, DEFAULT) gt
JOIN sys.trace_events te ON gt.EventClass = te.trace_event_id
WHERE EventClass in (164) --AND gt.EventSubClass = 2
ORDER BY StartTime DESC;
Using the stored procedure sp_msforeachtable it's possible to execute a script for all tables in a database.
However, there are system tables which I'd like to exclude from that. Instinctively, I would check the properties IsSystemTable or IsMSShipped. These don't work like I expect - I have for example a table called __RefactorLog:
But when I query if this is a system or MS Shipped table, SQL Server reports none of my tables are system tables:
exec (N'EXEC Database..sp_msforeachtable "PRINT ''? = '' + CAST(ObjectProperty(Object_ID(''?''), ''IsSystemTable'') AS VARCHAR(MAX))"') AS LOGIN = 'MyETLUser'
-- Results of IsSystemTable:
[dbo].[__RefactorLog] = 0
[schema].[myUserTable] = 0
and
exec (N'EXEC Database..sp_msforeachtable "PRINT ''? = '' + CAST(ObjectProperty(Object_ID(''?''), ''IsMSShipped'') AS VARCHAR(MAX))"') AS LOGIN = 'MyETLUser'
-- Results of IsMSShipped:
[dbo].[__RefactorLog] = 0
[schema].[myUserTable] = 0
When I look into the properties of the table (inside SSMS), the table is marked as a system object. An object property like IsSystemObject doesn't exist though (AFAIK).
How do I check if a table is a system object, apart from the object property? How does SSMS check if a table is a system object?
Management studio 2008 seems to run some quite ugly following code when opening the "System Tables" folder in the object explorer, the key bit seems to be:
CAST(
case
when tbl.is_ms_shipped = 1 then 1
when (
select
major_id
from
sys.extended_properties
where
major_id = tbl.object_id and
minor_id = 0 and
class = 1 and
name = N''microsoft_database_tools_support'')
is not null then 1
else 0
end
AS bit) AS [IsSystemObject]
(Where tbl is an alias for sys.tables)
So it seems that it's a combination - either is_ms_shipped from sys.tables being 1, or having a particular extended property set.
__refactorlog is, in contrast to what SSMS suggests, a user table. It is used during deployment to track schema changes that cannot be deduced from the current database state, for example renaming a table.
If all your other user tables are in a custom (non-dbo) schema, you can use a combination of the isMSshipped/isSystemTable attributes and the schema name to decide if a table is 'in scope' for your script.
In the past I've worked on the assumption that, in the sys.objects table, column is_ms_shipped indicates whether an object is or is not a system object. (This column gets inherited by other system tables, such as sys.tables.)
This flag can be set by procedure sp_ms_markSystemObject. This, however, is an undocumented procedure, is not supported by Microsoft, I don't think we're supposed to know about it, so I didn't tell you about it.
Am I missing something?
However, there are system tables which I'd like to exclude from that
At least on SQL Server 2008, sp_MSforeachtable already excludes system tables, as this excerpt from it shows:
+ N' where OBJECTPROPERTY(o.id, N''IsUserTable'') = 1 ' + N' and o.category & ' + #mscat + N' = 0 '