Currently in middle of migration from SQL Server to Oracle. Whats the best practices that i should applied across?
And we also encounter some problem like the dateadd functions not working in oracle.
MSSQL Code
USE [TEST]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[GET_MONTHS_LAST_DAY](#MON int)
RETURNS DATETIME
AS
BEGIN
RETURN DATEADD(dd, -DAY(DATEADD(m,1,getdate())), DATEADD(m,-MON+1,datediff(dd,0,getdate())))
END
Converted Oracle
create or replace
FUNCTION GET_MONTHS_LAST_DAY
(
v_MON IN NUMBER
)
RETURN DATE
AS
BEGIN
RETURN utils.dateadd('DD', -utils.day_(utils.dateadd('M', 1, SYSDATE)), utils.dateadd('M', -v_MON + 1, utils.datediff('DD', 0, SYSDATE)));
END;
Any idea why i cannot compile the oracle functions? The only thing i see here is the dateadd functions are not available in oracle. Thanks.
Your script looks like Oracle SQL Developer automatic conversion script.
You need generate package utils. Yo can use button "generate utils package" ( 2nd button in capture)in util window.
Related
SQL Server 2012 - I have a view (complicated) and one of the columns needs to have anything non-numeric stripped out. The following works to a point;
STUFF(dbo.campaign_tracking_clicks.tt_cpn, 1, PATINDEX('%[0-9]%', dbo.campaign_tracking_clicks.tt_cpn) - 1, '') AS emailerid
I get an error if anything but numbers are at the end of the value.
I have a scalar-valued function'
/****** Object: UserDefinedFunction [dbo].[KeepNumCharacters] Script Date: 10/11/2016 1:05:51 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Function [dbo].[KeepNumCharacters](#Temp VarChar(100)) Returns VarChar(100)
AS
Begin
While PatIndex('%[^0-9]%', #Temp) > 0
Set #Temp = Stuff(#Temp, PatIndex('%[^0-9+]%', #Temp), 1, '')
Return #TEmp
End
I'm using;
dbo.KeepNumCharacters(dbo.campaign_tracking_clicks.tt_cpn) AS emailerid
But, it's taking a very long time to execute. I've searched and searched but without finding an alternative.
Yes, scalar user-defined functions often make queries slow. Sometimes very slow.
See for example T-SQL User-Defined Functions: the good, the bad, and the ugly.
In your case I don't see how to rewrite the scalar function into an inlined table-valued function.
One option is to add an extra column to your table that would hold the result of your scalar function calculations. You can write a trigger that would keep its content in sync with the main column as the main column changes.
It will slow down the updates and inserts, but it will speed up your SELECT queries.
I am building a webservice in ASP.NET that will call a stored procedure to get all the cases between two dates. I am using SQL Management Studio Here is my code:
ALTER PROCEDURE spGetNewCases
(#startDate datetime, #endDate datetime)
AS
BEGIN
SELECT
caseID
FROM
tblCases
WHERE
dateOpened BETWEEN #startDate and #endDate
END
GO
EXEC spGetNewCases
However, I get the following error:
"Could not find object spGetNewCases".
In the IDE , the words "between" and "and" are highlighted in light gray instead of dark blue. Moreover, I would like to test it with two dates, say 2015-01-01 and 2015-01-04.
The format of my datetime in my SQL server is datetime2((0),null). E.g.
2015-01-02 00:00:00.
I would greatly appreciate the community's feedback. Thank you!
EDIT:
Thank you everyone for your great help! Here is my new code that works!
-- procedure
USE [vetDatabase_Wizard]
GO
ALTER PROCEDURE sp_GetNewCases (#startDate datetime, #endDate datetime)
AS
BEGIN
SELECT caseID FROM tblCases
WHERE dateOpened BETWEEN #startDate and #endDate
END
GO
EXEC sp_GetNewCases '2015-01-01', '2015-04-04'
Check your database
USE CASE_DB
GO
Qualify your schema name.
ALTER PROCEDURE schema.spGetNewCases ...
See if that doesn't find it...
EDIT
IF you're using SSMS -- right-click your stored procedure and select 'modify'. That will give you a pre-written ALTER template that you can then modify.
If your SP is created then check your DB context. In SSMS there is a dropdown menu, you can also USE DBNAME. In ASP code it is in your connection string.
If it is not created you will need to Create Procedure first and then you can Alter Procedure.
You can call the SP like so : EXEC spGetNewCases '2016-03-01', '2016-05-01'
Side note: System Stored Procedures use the sp prefix you probably should choose something different like sp_ or usp.
I'm new to SSIS so forgive me if this question is trivial or has already been answered. So I have a SQL query that begins as follows:
declare #start datetime, #end datetime, #startMonth datetime, #endMonth datetime, #maxHoursToRespond int
----Set These------------------------------------------------
set #end='6/27/2014'
set #maxHoursToRespond=24
-------------------------------------------------------------
set #start=dateadd(dd, -90, #end) -- set duplication period
set #startMonth=dateadd(dd, -2, #end)-- set to start date of output you want
set #endMonth=dateadd(dd, -1, #end) -- set to day of end date of output you want
When I put this in my OLE DB Source Editor with SQL command as my data access mode,
all the variables are replaced with question marks.
It looks like :
DECLARE ? datetime, ? datetime, ? datetime, ? datetime, ? int
/*--Set These------------------------------------------------*/ SET ? = ?
SET ? = 24
/*-----------------------------------------------------------*/ SET ? = dateadd(dd, - 90, ?)
SET ? = dateadd(dd, - 2, ?)
SET ? = dateadd(dd, - 1, ?)
In the query builder.I'd like to know why this is happening.I'd also like to know how I can allow the query to be successfully built
(currently I get a syntax error of "The Declare SQL construct or statement is not supported.").
Do I have to create these variables (like #start) in SSIS itself?
You can:
Encapsulate your SQL in a stored procedure that contains all of the declarations internally (assuming the variable values are static), then call the stored procedure in the execute SQL task with EXEC. EXEC My_Stored_Procedure
Write a stored procedure that accepts the variables as inputs, map them to variables in SSIS, then execute the stored procedure like this Exec My_Stored_Proc ?,?,?,? with the user variables mapped to the corresponding stored procedure variables.
Leave the query as is, but remove the DECLARE and the SETs, and map the SSIS variables to the query. This most likely will not work, because SSIS will not know which ? corresponds with which variable (it will try to map them in the order they appear.
Number 2 is the generally accepted method, unless you store your variable values in a table or something that the SP in number can access, in which case number 1 may be cleaner.
I have pasted straight to SQL command text yours script in OLE DB Source Editor and nothing changed in it. By pressing Parse Quesry.. I checked that SQL is correct. When I tried to use Build Query.., it said, that DECLARE is not supported. So don't use builder :)
I build a function and I am trying to execute it...but some errors are occurring
CREATE FUNCTION dbo.Afisho_rankimin(#emri_rest int)
RETURNS int
AS
BEGIN
Declare #rankimi int
Select #rankimi=dbo.RESTORANTET.Rankimi
From RESTORANTET
Where dbo.RESTORANTET.ID_Rest=#emri_rest
RETURN #rankimi
END
GO
SELECT dbo.Afisho_rankimin(5)AS Rankimi
GO
The errors when I execute it are:
Msg 2714, Level 16, State 3, Procedure Afisho_rankimin, Line 11
There is already an object named 'Afisho_rankimin' in the database.
and also it is said that:
Can not find column "dbo", or the user defined function, or aggregate "dbo.Afisho_rankimin", or the name is ambiguous
It looks like there's something else called Afisho_rankimin in your DB so the function is not being created. Try calling your function something else. E.g.
CREATE FUNCTION dbo.Afisho_rankimin1(#emri_rest int)
RETURNS int
AS
BEGIN
Declare #rankimi int
Select #rankimi=dbo.RESTORANTET.Rankimi
From RESTORANTET
Where dbo.RESTORANTET.ID_Rest=#emri_rest
RETURN #rankimi
END
GO
Note that you need to call this only once, not every time you call the function. After that try calling
SELECT dbo.Afisho_rankimin1(5) AS Rankimi
I have come to this question and the one below several times.
how to call scalar function in sql server 2008
Each time, I try entering the Function using the syntax shown here in SQL Server Management Studio, or SSMS, to see the results, and each time I get the errors.
For me, that is because my result set is in tabular data format. Therefore, to see the results in SSMS, I have to call it like this:
SELECT * FROM dbo.Afisho_rankimin_TABLE(5);
I understand that the author's question involved a scalar function, so this answer is only to help others who come to StackOverflow often when they have a problem with a query (like me).
I hope this helps others.
you may be create function before so,
update your function again using.
Alter FUNCTION dbo.Afisho_rankimin(#emri_rest int)
RETURNS int
AS
BEGIN
Declare #rankimi int
Select #rankimi=dbo.RESTORANTET.Rankimi
From RESTORANTET
Where dbo.RESTORANTET.ID_Rest=#emri_rest
RETURN #rankimi
END
GO
SELECT dbo.Afisho_rankimin(5) AS Rankimi
GO
I have a function and select statement
CREATE FUNCTION dbo.fun_currentday ( #dt DATETIME)
RETURNS DATETIME
AS
BEGIN
DECLARE #currentday DATETIME
SET #currentday = DATEADD(dd, DATEDIFF(dd, 0, #dt), 0)
RETURN (#currentday)
END
GO
DECLARE #pvm AS DATETIME
SET #pvm = GETDATE()
SELECT 'Last 7 days' AS Range, dbo.fun_currentday(#pvm) - 6 AS Stday, dbo.fun_currentday(#pvm) AS Endday
All works fine but when I hover over dbo.fun_currentday at the select statement, I get an error saying:
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.fun_currentday", or the name is ambiguous.
Where's the problem?
Intellisense / Error highlighting always does this for newly created objects. Use Ctrl+Shift+R to refresh the local cache.
Before
After
Everything runs fine here with SQL Server 2008 Express.
Have you tried running your query on a different database?
You can try to create a new schema and create you UDF in it.
Make sure you have the necessary permissions and that the dbo schema configuration is correct.
Your stored procedure have to be stored somewhere so when you don't specify the location it goes to default database (master). So you should call it like
SELECT 'Last 7 days' AS Range,master.dbo.fun_currentday(GETDATE()) - 6 AS Stday, master.dbo.fun_currentday(GETDATE()) AS Endday
EDITED
I've checked that and I wasn't right, it not always goes to mater schema. It goes to database in contex of you were woring, so if your create procedure was created in query on root folder it goes to master, but if you created it in query of test databse you should use test.dbo.fun_currentday(GETDATE()). To avoid it always specify the databse like USE database_name GO CREATE FUNCTION dbo.fun_currentday