SQL Delta dependency circular reference - sql-server

SQL delta tool shows warning for my SP. It says that there is a potential dependency circular reference. How to find out the issue ?
i used the following script to check the issues with tables :
SELECT
OBJECT_SCHEMA_NAME(fk1.parent_object_id)
+ '.' + OBJECT_NAME(fk1.parent_object_id),
OBJECT_SCHEMA_NAME(fk2.parent_object_id)
+ '.' + OBJECT_NAME(fk2.parent_object_id)
FROM sys.foreign_keys AS fk1
INNER JOIN sys.foreign_keys AS fk2
ON fk1.parent_object_id = fk2.referenced_object_id
AND fk2.parent_object_id = fk1.referenced_object_id;
so far no result. It seems the issue with SP

Related

SQL Server trigger reports 'inserted' table is missing

I am running a script against a SQL Server 2016 database that creates various tables, views and triggers. This same script has been working against dozens of other servers but it is getting an error against this one particular server.
All of the triggers seem to be created but when I check for invalid objects it reports all of them as invalid. The really strange part is, it says the problem is the "inserted" table (or "deleted" table, depending on the trigger) is missing.
I am checking for invalid objects using this query:
SELECT
QuoteName(OBJECT_SCHEMA_NAME(referencing_id)) + '.'
+ QuoteName(OBJECT_NAME(referencing_id)) AS ProblemObject,
o.type_desc,
ISNULL(QuoteName(referenced_server_name) + '.', '')
+ ISNULL(QuoteName(referenced_database_name) + '.', '')
+ ISNULL(QuoteName(referenced_schema_name) + '.', '')
+ QuoteName(referenced_entity_name) AS MissingReferencedObject
FROM
sys.sql_expression_dependencies sed
LEFT JOIN
sys.objects o ON sed.referencing_id = o.object_id
WHERE
(is_ambiguous = 0)
AND (OBJECT_ID(ISNULL(QuoteName(referenced_server_name) + '.', '')
+ ISNULL(QuoteName(referenced_database_name) + '.', '')
+ ISNULL(QuoteName(referenced_schema_name) + '.', '')
+ QuoteName(referenced_entity_name)) IS NULL)
ORDER BY
ProblemObject,
MissingReferencedObject
which I got from here
Find broken objects in SQL Server
The triggers are "instead of" triggers against the views that then modify the underlying tables. There is really nothing special about any of them.
Is there something wrong with my query and the objects aren't really invalid or is there something with the database? I am running the script as the database owner so I don't think it is a permissions issue.
Thanks
Run this query against the problem SQL Server, and against one where no problems are shown:
SELECT *
FROM
sys.sql_expression_dependencies sed
LEFT JOIN
sys.objects o ON sed.referencing_id = o.object_id
WHERE type = 'TR'
ORDER BY
type_desc,
referenced_entity_name
If the inserted / deleted tables show up in one list but not the other, then there is some difference causing this. I'd check identifier quoting rules to start.

SQL - WHERE statement with text added to value from table

I need to copy data from an old database to a newer one.
Both of these databases have a user setup table with the primary key of "USER ID".
The problem is, in the old database the users didn't have the domain in the name, but in the new one they have.
Example:
Primary Key old DB: USER1
Primary Key new DB: DOMAIN\USER1
This prevents a standard WHERE clause to update the correct user because it can't find it due to the domain being added.
My code:
'FROM [' + #src_DB + '].dbo.[' + #src_table + '] as src '
'WHERE [' + #dest_DB + '].dbo.[' + #dest_table + '].[User ID] = ' + #domain_name + 'src.[User ID]'
printing the result:
WHERE [Destination_DB].dbo.[Destination_Table].[User ID] = DOMAIN\src.[User ID]
The problem is it doesn't add the DOMAIN to the value but rather to the statement...
How can I add the Domain to the actual value of src.[User ID]?
I think there's a dot missing, and you should use QUOTENAME
'WHERE ' + QUOTENAME(destination_table) + '.[User ID] = ' + QUOTENAME(#domain) + '.' + QUOTENAME(source_table) + '.[User ID]'
Whenever you create a SQL statement dynamically it's a good idea to print it out, copy it into a new query window and check for syntax errors...
UPDATE You: Yes, both databases are in the same server
An object can be (fully) specified with
ServerName.DatabaseName.Schema.ObjectName
A table's column would add one more .ColumnName
When both objects live on the same server you can let the first part away.
Objects of the same database let this part away.
Objects of the default schema might be called with the ObjectName alone.
But if you state a DatabaseName you must also state a SchemaName!
Use QUOTENAME() to add the brackets and add just the dots via string concatenation (or use CONCAT()-function).
UPDATE 2 Did I get this wrong completely?
After you comment I think I understand it now: You want to compare the values of both [USER ID] columns, but the new is DOMAIN\MyUserId while the older was just MyUserId.
You have two approaches
Add the Domain\ as string to the value of [User ID]
Use SUBSTRING([User ID],CHARINDEX('\',[UserID])+1,1000) to cut the newer value down to the naked value of [User ID]
For the first something like this
'WHERE [' + #dest_DB + '].dbo.[' + #dest_table + '].[User ID] = ''' + #domain_name + ''' + src.[User ID]'
The second is quite clumsy with dynamically created SQL...

Stored Procedure errors out only when calling from a trigger

I am using SQL Server to programming something that's triggered by a UPDATE even in a table.
So the structure is like: in the trigger, I have:
EXEC StoredProcedure1
And in StoredProcedure1 I have:
INSERT dbo.UnbalancedFreqStops
SELECT APremise AS PremiseID
,geography::ConvexHullAggregate(Location) AS Shape
FROM
(SELECT
A.[Premise ID] AS APremise, B.[Premise ID] AS BPremise,
B.LatLon AS Location, B.[Service Frequency] AS FreqT, B.Mon_Routed AS MonT,
B.Tue_Routed AS TueT, B.Wed_Routed AS WedT, B.Thu_Routed AS ThuT,
B.Fri_Routed AS FriT, B.Sat_Routed AS SatT, B.Sun_Routed AS SunT
FROM
dbo.Weekly AS A
INNER JOIN
dbo.Weekly AS B ON A.LatLon.STDistance(B.LatLon) <= (80 * 2)
) AS Clusters
GROUP BY
APremise
HAVING
MAX(FreqT) <> (MAX(MonT) + MAX(TueT) + MAX(WedT) + MAX(ThuT) + MAX(FriT) + MAX(SatT) + MAX(SunT));
It's interesting that it errors out with violation of primary key error (PremiseId being unique PK) ONLY WHEN I am calling the stored procedure from the trigger. And the stored procedure runs fine if I (1) run it alone, or (2) call it from another stored procedure.
Moreover, I ran the query alone, and found there is NO duplicate key AT ALL.
Can somebody enlighten me?
Thanks!

Remove permission from public

please see snap shoot below.I was wondering why I can't remove the permission with following code. But I can remove the permission by using the GUI.
REVOKE ALL ON OBJECT::[dbo].[Table1] FROM [PUBLIC];
It tested this on SQL Server 2005, and the query works fine. However, do not expect the table Properties dialog to update automatically, not even after moving forward and back to another page, like Storage. You must close and reopen the dialog to see the modifications.
I checked this by running the following script in steps:
Source: How to list permissions for Public Role for a database in SQL Server.
grant select, insert, update on dbo.Table1 to public
GO
SELECT a.[name] + ' ' + v.[name] + ' ON ' + QuoteName(oo.[name])
+ '.' + QuoteName(o.[name]) + ' TO ' + QuoteName(u.[name])
FROM dbo.sysprotects AS p
JOIN master.dbo.spt_values AS a
ON (a.number = p.protecttype
AND 'T' = a.type)
JOIN master.dbo.spt_values AS v
ON (v.number = p.action
AND 'T' = v.type)
JOIN dbo.sysobjects AS o
ON (o.id = p.id)
JOIN dbo.sysusers AS oo
ON (oo.uid = o.uid)
JOIN dbo.sysusers AS u
ON (u.uid = p.uid)
WHERE 'public' = u.name
GO
revoke all on object::dbo.Table1 to public
GO
-- Run query again
Please note that a warning is issued when using revoke all: The ALL permission is deprecated and maintained only for compatibility. It DOES NOT imply ALL permissions defined on the entity.

How to filter values from a query against a SQL Server database?

I've a query that works OK against a MySQL database, but we've had to migrate the database and applications to SQL Server. Now I'm in trouble getting this query to work.
SELECT
complist.CompName,
complist.CompID,
componenttrace.Remark,
complist.McID,
complist.Station,
complist.Slot,
complist.Amount - complist.Used - ISNULL(complist.Correction,0)
FROM
complist, componenttrace
WHERE
complist.McID = 1004
AND complist.CompID = componenttrace.CompID
AND UPPER(complist.Station + '.' + complist.Slot) LIKE '%1.1%'
ORDER BY
complist.CompName, complist.CompID
On the application, the part that goes
AND UPPER(complist.Station + '.' + complist.Slot) LIKE '%1.1%'
is added automatically if there's any value on a given field (i.e. 1.1).
I have a SQL syntax problem, but don't know how to solve it. Can anyone shed some light here?
thanks,
Gustavo
Without error message is very hard to say what is wrong, but my bet is on the implicit join. Try
SELECT
complist.CompName,
complist.CompID,
componenttrace.Remark,
complist.McID,
complist.Station,
complist.Slot,
complist.Amount - complist.Used - ISNULL(complist.Correction,0)
FROM
complist
INNER JOIN componenttrace ON complist.CompID = componenttrace.CompID
WHERE
complist.McID = 1004
AND UPPER(complist.Station + '.' + complist.Slot) LIKE '%1.1%'
ORDER BY
complist.CompName, complist.CompID

Resources