SQL Server 2000 Error Creating Function - sql-server

I can't create a function in SQL Server 2000 in my database. So, I tried to use the example in the help (F1), but I have the same error.
CREATE FUNCTION CubicVolume
-- Input dimensions in centimeters
(#CubeLength decimal(4,1),
#CubeWidth decimal(4,1),
#CubeHeight decimal(4,1)
)
RETURNS decimal(12,3) -- Cubic Centimeters.
AS
BEGIN
RETURN ( #CubeLength * #CubeWidth * #CubeHeight )
END
Errors:
Msg 170, Level 15, State 1, Line 2 Line 2:
Incorrect syntax near 'FUNCTION'.
Msg 137, Level 15, State 1, Line 9
Must declare the variable '#CubeLength'.
I run this script for Database Northwind and it didn't have problem, so I don't understand what the problem is.

thank you for your answers.
The problem was resolved.
The problem was compatibility on the level as Aaron Bertrand said.
Click Right on Database -->Properties-->Options-->Compatibility Level
(Needs 80 or high)
enter image description here

Related

Sometimes I get errors when calling GO in Scripts in SQL Server Management Studio

When I follow this alter function with a GO, I get a syntax error near the GO. Without the GO, it runs correctly.
I want to add those GO because I'm writing an script altering 50 functions (alter function needs to be the first instruction of a batch). The vast majority run correctly, but four of them raise the error on their GO line, like in this example.
ALTER FUNCTION dbo.f_cost_standar
(#article varchar(24),
#dataCost Datetime)
RETURNS decimal(18,5)
AS
BEGIN
DECLARE #resultat decimal(18,5)
SET #resultat = NULL
SELECT TOP 1 #resultat = preu
FROM costos_Standar
WHERE article = #article
AND #dataCost BETWEEN dataInici AND ISNULL(dataFinal, '31/12/2099')
ORDER BY dataInici DESC
RETURN #resultat
END;
GO
This is the error I get:
Msg 102, Level 15, State 1, Procedure f_cost_standar, Line 12 [Batch Start Line 0]
Sintaxis incorrecta cerca de 'GO'.
When does the go fail, and what can I do to fix it?
PS: Thanks to #JeroenMostert I've seen that the problem was a broken newline before the GO. Now it works nicely.

Calling stored procedure from code causes error

I am trying to call a stored procedure from code using .Net Core 3.0 and SQL Server 2012, but I keep getting this error:
SqlException: Incorrect syntax near the keyword 'exec'. Incorrect syntax near ')'.
My code:
var policycontacts = await _dbContext.PolicyContacts
.FromSqlInterpolated($"exec dbo.spapi_contact_getbypolicy {input}")
.FirstOrDefaultAsync()
.ConfigureAwait(false);
SQL code:
EXEC Sp_executesql
N'SELECT TOP(1) [p].[ID], [p].[ActionsToTake],
[p].[AddedBy], [p].[BirthCountry], [p].[ContactGUID],
[p].[ContactID], [p].[ContactStatus], [p].[DOBFormation],
[p].[Domicile], [p].[EnhancedDueDiligence], [p].[EntityName],
[p].[EstimatedAmountAssets], [p].[FirstName], [p].[Gender],
[p].[HowClientMet], [p].[LastModifiedBy], [p].[LastModifiedDate],
[p].[LastName], [p].[LastOpenDate], [p].[LastOpenedBy],
[p].[MiddleName], [p].[Notes], [p].[OccupationBusiness],
[p].[OnlineUser], [p].[OpeningNotes], [p].[OriginAssets],
[p].[PEP], [p].[PEPDescription], [p].[PersonalSituation],
[p].[RiskOverride], [p].[Sysdate] FROM (exec dbo.spapi_contact_getbypolicy #p0)AS [p]',
N'#p0 nvarchar(4000)',
#p0=N''
Complete error:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'exec'
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ')'
Not enough rep to leave a comment, so I have to leave you an answer. Sorry, I think this is more of a comment.
I have a "okay" recommendation, but it might not be the best answer. Depending on how complex your stored procedure is and if you really want to call it in the FROM section, then change your stored procedure into a table valued function. The reason why its a terrible recommendation is there are limitations and performance issues by using a function. I'd read up on the following articles and see which route you would like to take.
Table Valued Function Killing My Query Performance
https://blogs.msdn.microsoft.com/psssql/2010/10/28/query-performance-and-multi-statement-table-valued-functions/
Cross Apply with table valued function article:
https://www.sqlshack.com/the-difference-between-cross-apply-and-outer-apply-in-sql-server/
The other option could be the one where Sean Lange provided.

CASE STATEMENT for create view in SQL Server 2008

I have a create view query and I want to check if it does not exist yet, then create view. I tried to create like this:
CASE WHEN IS NOT EXISTS vw_Delays
THEN
VIEW vw_Delays AS
SELECT RD_RfileID_fk_ind, SUM(DATEDIFF(day, RD_Startdate, RD_EndDate)) AS delays FROM dbo.t_RfDelay
GROUP BY RD_RfileID_fk_ind
END
but it returns these errors:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'CASE'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'END'.
How to solve this? Please can anyone help me to fix this ?
You need to use this code to check for the view's existence:
IF NOT EXISTS (SELECT * FROM sys.views WHERE Name = N'vw_Delays')
.....
The next obstacle you'll encounter is the fact that the CREATE VIEW statement must be the first of a SQL batch - so you cannot have it right after the existence check.
What I usually do is the opposite:
check if the view does exist
and if so - drop the existing view
then create the view from scratch
and I use this code for this setup:
IF EXISTS (SELECT * FROM sys.views WHERE Name = N'vw_Delays')
DROP VIEW dbo.vw_Delays;
GO
CREATE VIEW dbo.vw_Delays
AS
SELECT
RD_RfileID_fk_ind,
SUM(DATEDIFF(day, RD_Startdate, RD_EndDate)) AS delays
FROM
dbo.t_RfDelay
GROUP BY
RD_RfileID_fk_ind

How to set data and log file location for SQL Database?

I'm trying to create a database where the data and log files are saved to the E drive but not sure how to do so. I've tried:
CREATE DATABASE TachographDataContent_Archive
[ON E:\MySqlDir\MSSQL.MSSQLSERVER\MSSQL\Data]
[LOG ON {D:\MySqlDir\MSSQL.MSSQLSERVER\MSSQL\Data}]
And I'm getting this error:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'TachographDataContent_Archive'.
To create database using query, you need to mention .mdf and .ldf file. So try below script.
Like this
CREATE DATABASE [TachographDataContent_Archive]
ON PRIMARY (NAME = 'TachographDataContent', FILENAME = 'E:\MySqlDir\MSSQL.MSSQLSERVER\MSSQL\Data\TachographDataContent.mdf')
LOG ON
(NAME = 'TachographDataContent_log', FILENAME = 'E:\MySqlDir\MSSQL.MSSQLSERVER\MSSQL\Data\TachographDataContent_log.ldf')
GO
good luck...

Error in SQL Server updating Image datatype from a linked server

This is a table in a Microft Dynamics 2009 database. Our Test database is missing a bunch of Image data, so I would like to update the table in test with the data in production. I'm using this SQL for this update. When I execute this, I get this error:
Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 306, Level 16, State 2, Line 1
The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.
Query:
UPDATE INVENTTABLE
SET
Z_IMAGE = i2.Z_IMAGE,
Z_IMAGEMIMETYPE = i2.Z_IMAGEMIMETYPE
FROM INVENTTABLE i1
JOIN [PRODSQLSERVER].[DAX2009DB].[dbo].INVENTTABLE i2
ON i1.RECID = i2.RECID
WHERE i2.Z_IMAGE IS NOT NULL
I can't see a place where I'm attempting to compare or sort the Image data.
Try changing UPDATE INVENTTABLE to UPDATE i1.

Categories

Resources