This code:
USE [db]
GO
/****** Object: UserDefinedFunction [dbo].[GetFieldPickerReports] Script Date: 01/11/2013 19:12:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[GetFieldPickerReports] (#CustomerName varchar(100))
RETURNS TABLE
AS
RETURN SELECT [fpr].[ID], [fpr].[Name], [fpr].[Global], [fpr].[FPRCategoryID]
FROM FieldPickerReport fpr, dbo.GetProductKeyIdByCustomer(#CustomerName) pk
LEFT JOIN [FPRCategory] fprcat on ([fpr].[FPRCategoryID]=[fprcat].[ID])
WHERE [Global]=1 OR ProductKeyID=pk.id
Produces an error:
The multi-part identifier "fpr.FPRCategoryID" could not be bound.
This query works without the Left Join (which I currently need to add) or either with the removal of dbo.GetProductKeyIdByCustomer() and the Where clause.
Here's the code for dbo.GetProductKeyIdByCustomer()
USE [db]
GO
/****** Object: UserDefinedFunction [dbo].[GetProductKeyIdByCustomer] Script Date: 01/11/2013 19:58:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[GetProductKeyIdByCustomer] (#CustomerName varchar(100))
RETURNS TABLE
AS
RETURN SELECT id
FROM ProductKey
WHERE [CustomerName]=#CustomerName AND Enabled=1
What is wrong with my query?
First, I think that these UDFs would be better implemented as Views.
Second, you are mixing implicit and explicit joins. Using all explicit joins should solve your issue:
SELECT
[fpr].[ID],
[fpr].[Name],
[fpr].[Global],
[fpr].[FPRCategoryID]
FROM
FieldPickerReport fpr
JOIN dbo.GetProductKeyIdByCustomer(#CustomerName) pk
ON [Global] = 1 OR ProductKeyID = pk.id
LEFT JOIN FPRCategory fprcat
ON fpr.FPRCategoryID = fprcat.ID
Related
I'm writing a script to create a view, only IF the view does not already exist. If the view does already exist, I don't want to alter it or drop and re-create it. The syntax below is obviously not complete, and generates an error because CREATE VIEW needs to be in its own batch - but what is the proper way to construct my use case?
IF OBJECT_ID('dbo.view_name') IS NULL
BEGIN
CREATE VIEW [dbo].[view_name]
AS
SELECT ...;
END
ELSE
...
SQL Server 2016 has CREATE OR ALTER.
CREATE OR ALTER VIEW vw_your_view
AS
SELECT 1 FROM your_Table
GO
This will blow up if you move it to an environment below SQL Server 2016. If that is the case, go with what you have and check for an obj ID.
I changed the SQL to be the following. To avoid the "CREATE VIEW must be in its own batch" error, I wrapped the "CREATE VIEW" inside an exec('') statement. This works!
USE [XXXXXXXXX]
GO
/****** Object: View [CXXXXXXX].[_MobileMessageTracking_v2] Script Date: 5/4/2018 3:19:04 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF Object_id('CXXXXXXX._MobileMessageTracking_v2') IS NULL BEGIN
exec('CREATE VIEW [CXXXXXXX].[_MobileMessageTracking_v2]
AS
SELECT
/* fields here */
FROM CXXXXXXXX._MobileMessageTracking AS M WITH (NOLOCK)
WHERE M.MID = XXXXXXX
AND M.CreateDateTime >= DATEADD(mm,-6, GETDATE())
UNION
select
/* fields here */')
END
GO
I am new in SQL server and having trouble with this trigger code. Can you please tell me what is wrong with this line: set NazivFirme=(select NazivFirme from inserted)... This is where I get an error.
USE [BazaPreduzece]
GO
/****** Object: Trigger [dbo].[promenaNazivaFirme] Script Date: 2017-03-28 5:29:47 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER trigger [dbo].[promenaNazivaFirme]
on [dbo].[Firma]
AFTER UPDATE
as
declare #FirmaID int
select #FirmaID=FirmaID from inserted
if update (NazivFirme)
begin
alter table Vlasnik disable trigger zabranaAzuriranjaNazivaFirme
update [dbo].[Vlasnik]
set NazivFirme=(select NazivFirme from inserted)
where FirmaID=#FirmaID
alter table Vlasnik enable trigger zabranaAzuriranjaNazivaFirme
end
That's theFirma and the Vlasnik tables...
I'm currently facing a strange situation.
I'm collecting code of existing stored procedures from a query to a TMP table.
TABLE:
##SPListAndCode
(
Code nVarchar(MAX)
)
Query:
INSERT INTO ##SPListAndCode
SELECT OBJECT_DEFINITION (OBJECT_ID('SPname')))
After that I am trying to replace values to get from Create query, Alter query
REPLACE(CODE, 'CREATE PROCEDURE', 'ALTER PROCEDURE')
But problem is this: REPLACE function is not replacing values.
But, when I am trying to use
REPLACE(CODE, 'CREATE', 'ALTER')
function works as expected.
But this scenario are not acceptable for me, because inside the stored procedure there can be things like
CREATE TABLE
Example data inside "Code" column:
/****** Object: StoredProcedure dbo.spName Script Date: 6/20/2016 9:10:18 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE dbo.spName
AS
DECLARE #pStartDate date, #x int
SET #pStartDate = (SELECT max(CT_ACTIVITY_DATE) FROM Table)
...
Thanks a lot in advance for any kind of support!
Your stored procedure has two spaces between CREATE and PROCEDURE, while your replace is looking for the string with a single space between the words.
To gain access to the actual code contained inside of the stored procedures, you can use something like this:
SELECT
so.name [ObjectName], so.type,
OBJECT_NAME(sc.id), sc.id, sc.colid , sc.[text]
FROM
sys.syscomments sc
INNER JOIN
sys.sysobjects so ON so.id = sc.id
WHERE
so.type = 'P'
ORDER BY
sc.id, sc.colid
Note there can be multiple entries for each object, and the colid is used to order those entries.
How to check if function exist in Sybase? And if yes then drop?
Like I can check for stored procedure:
/****** Object: StoredProcedure Sp_Name Script Date: 05/18/2015 16:33:46 ******/
IF EXISTS (SELECT * FROM sysobjects WHERE NAME = 'Sp_Name' AND TYPE='P')
DROP PROCEDURE Sp_Name
GO
Do either of the following:
IF OBJECT_ID('my_function') IS NOT NULL
DROP FUNCTION my_function
Or
IF EXISTS (
SELECT 1
FROM sysobjects
WHERE NAME = 'my_function'
AND TYPE = 'SF'
)
DROP FUNCTION my_function
GO
Thanks,
Meet
I have a trigger that is firing with out issue in non-production but the same exact code is failing to fire in production. I've confirmed it's not disabled and I've done a trace to see that it is not even executing in production.. I've checked the relevant OBJECTPROPERTY elements and they are the same. I've confirm the code is the same. I've confirmed the same inserts are coming from the application. Below is the code for this trigger:
/******
Object: Trigger [dbo].[tr_trigger_ins]
Script Date: 06/02/2012 16:51:51
******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE TRIGGER [dbo].[tr_trigger_ins] ON [dbo].[table_1]
FOR INSERT
AS
BEGIN
UPDATE table_2
SET col_1 =
CASE
WHEN i.col_2 = '0' THEN 0 ELSE 1
END
FROM INSERTED i
INNER JOIN table_3 pa
ON i.col_3 = pa.col_3 AND pa.col_4 = 'ispublic'
INNER JOIN table_4 pp
ON i.col_5 = pp.col_5
INNER JOIN table_2 cs
ON pp.col_6 = cs.col_6
END
GO
Below is a trigger that is an instead of insert on the same table that is executing in both environments:
/******
Object: Trigger [dbo].[tr_trigger_before_ins]
Script Date: 06/02/2012 16:55:52
******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE TRIGGER [dbo].[tr_trigger_before_ins] ON [dbo].[table_1]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO table_1
SELECT * FROM INSERTED
WHERE col_3 in (73, 199)
END
GO
Any help is greatly appreciated.
First make sure the trigger hasn't been disabled:
ENABLE Trigger tr_trigger_ins ON dbo.table_1;
GO
Ultimately it came down to the issue of replication. Transactions flowed over in a different order than expected. Once we accommodated for this in the trigger logic it worked well.