NULL Reference Exception when generating DACPAC with particular Scalar UDF - sql-server

I have a SQL Server 2017 database with a particular UDF that is giving me an error when trying to export a dacpac either via SSMS or sqlpackage. VisualStudio also throws the error when working in ssdt. The function works, and no issues there. included here is a dumbed down version of the function that has the same issues. You can create an empty database, add the function, and dacpac extract will fail. I understand there are poor design choices here, but this is just an example of the script, that still reproduces the error.
Function:
CREATE FUNCTION [dbo].[testFunction]
(
#string varchar(max)
)
RETURNS bit
AS
BEGIN
DECLARE #Valid BIT = CASE WHEN EXISTS (
SELECT *
FROM STRING_SPLIT(#string, ',') s
OUTER APPLY (select LEN(s.value) AS l) AS l
)
THEN 0 ELSE 1 END
RETURN #Valid
END
It literally just takes a comma separated string, splits it, does an outer apply to generate a length(I know, stupid IRL, but demo).
However, when attempting to extract a dacpac it gives error "Object reference not set to an instance of an object."
If I change the select * to select s.value, or select 1, then voila, dacpac generates and visual studio doesn't crash.
Anyone have any ideas as to why the select * in the above code would cause the dacpac to fail? We're evaluating using ssdt for source control, but random stuff like the above doesn't give me good feelings about using it, when it apparently is doing more code validation than the sql engine itself.
Environments:
SQL Server 2017 Standard
VS 2019 community with SSDT installed
SQLPackage Build 15.0.4826.1

I bet you are affected by following bug in SSDT:
SSDT failing to compile if STRING_SPLIT is used
It should be resolved few years ago, please check if you are using DACFX runtime 17.0.1 or newer

If you are using the VS function Extract Data-tier Application, you can uncheck Verify Extraction. I had to do this when using a dacpac to restore an Azure SQL DB Managed Instance database that had views referencing another database to an on-premise server. Otherwise it would appear to create a valid multi-gigabyte dacpac that could not be deployed on-premise with the same error you are getting.

Related

How do I test whether a given SQL Syntax is valid for an older version of SQL Server

I regularly end up developing on projects where either the Central Test server, or Prod server is an older version of SQL Server than my local install.
e.g. Prod is SQL Server 2014. Local install in SQL Server 2019.
Mostly that's fine, it just means I have to remember to only use old syntaxes. (:cry:)
But occasionally I forget which syntaxes are old. Ooops.
Obviously our test environments catch this, but it would be great to be able to tell my Local Server ... "only accept SS2014 syntax", and have these mistakes caught before they're committed/pushed.
I thought this was what CompatibilityLevel was supposed to do. But either it doesn't, or I'm using it wrong. (See below)
How should I achieve this? (other than just installing a different SQL version!)
Attempt so far:
Run ALTER DATABASE MyLocalProjectDB SET COMPATIBILITY_LEVEL = 120; (120 represents SS2014)
Run SELECT name, compatibility_level FROM sys.databases WHERE name = db_name(); to confirm that the previous command "worked".
Run DROP TABLE IF EXISTS [ATableThatDoesExist], to see if the syntax is accepted. It was.
DROP IF EXISTS was new to SS2016:
MSDN: IF EXISTS ... Applies to: SQL Server ( SQL Server 2016 (13.x) through current version).
Additional Source
Why hasn't this worked?

Schema compare - Unexpected exception caught during population of source model: Object reference not set to instance object

I've been running a schema compare in a database project in Visual Studio 2017, when I do this I get the following message in the Error List tab:
Unexpected exception caught during population of source model: Object
reference not set to instance object.
I found this blog, which appears to be the same issue, but the suggested solution (of removing the entry using the Select Target Schema window) has not worked despite trying it a few times.
The compare has (and does) work fine with the same project and database in Visual Studio 2013, so I have a work around, but it would be nice to know what is causing the problem (and leave VS2013 behind!)
I found a solution to this; for database projects there is a 'Target platform' setting in the properties (see below). I set this to SQL Server 2017 and the compare now works.
The default platform required appears to depend on the compatibility of the database (see https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-compatibility-level); while I required 2017 when I initially encountered this problem, a recurrence (against a database with a compatibility level of 120) needed SQL Server 2014 to be selected.
Oddly I have now seen that just switching the target platform back and forth can solve the problem e.g. I have a database project with SQL commands which were not present in SQL 2014, I ran the compare with a target of 2017 and it failed with the above error, ran with a 2014 target and it errors (as you would expect, since it does not understand the newer SQL functions), switch back to the 2017 target and the compare now works fine!
Edit: different job, different DB version (2019). Has all been working fine for months then this error cropped up. the above didn't work this time, so just in case anyone finds the same, the tried and tested closing and reopening VS sorted it!

Error when using parameters with SSIS in Visual Studio

I have a simple query (included below). It's used inside a loop with two parameters. It works as a charm on one server, but when I use it with another server, it fails with "Parameters cannot be extracted from the SQL command".
DECLARE #Tagnavn As varchar(250)
DECLARE #HentEnergiNiveauEfter As datetime2(7)
set #Tagnavn = ?
set #HentEnergiNiveauEfter = ?
EXEC [FDTV_PUMP_AVERAGE_CONSUMPTION_FROM_DATE] #Tagnavn ,#HentEnergiNiveauEfter
I've seen the workaround with putting the query in a variable, but this doesn't work for me. And I really don't understand why the above works on one server but not another :-)
The working set-up consists of:
-Microsoft SQL Server Data Tools for Visual Studio 2017, v15.3.3
-the stored procedure is on an SQL Server v11
The non-working set-up consists of:
-Visual Studio Community 2017 - Tools for Applications 2017, v15.7.3
-the stored procedure is on an SQL Server v10
Thanks!
Palle
Problem solved. Permission problems with the server holding the stored procedure. Thanks for your input. And #Larnu: now it works without the temp. variables :-)
Thanks again,
Palle

SQL Server 2012 error: object reference not set to an instance of an object

I use SQL Server 2012 and I have some databases on it. The problem is I suddenly get an error saying,
object reference not set to an instance of an object
I get this error when:
Going to write a new query
Select previously entered data by right click the table name ->
Select top 1000 rows
What I can do without getting error message:
Log into my instance successfully using both Windows Authentication mode and SQL Authentication mode.
Edit the table data by right click the table name -> Edit top 200 rows
Create a new database
I'm using:
Microsoft SQL Server Management Studio: 11.0.3128.0
Microsoft .NET Framework: 4.0.30319.34014
Operating System: Windows 8.1
Here are some snapshots of the error.
Please give me a solution to fix this problem. Your help will be highly appreciated.
I fixed the problem by running SSMS as administrator.
I could solve the error.
Repair the SQL Server.
Go to Add/remove programs Microsoft SQL Server 2012(x64) -> Uninstall/Change -> Repair.
Select the instance that you want to repair here.
For me, after repairing the instance, the error was solved.
Thanks for all who spent their valuable time to reply my question.
I got the same error message. Problem was 0 bytes free on the C: drive.
Its may be late, but i get the same error in SQL SERVER 2016, i resolved it by assigning full access to the back up folder.
I faced the same error once in my project. This is purely due to SSMS(sql client) is corrupted. Just for cross check that server is fine and client is corrupted, try to connect to the sql-server in this machine from any other server if you have access and query the tables. If it returns data, just uninstall and reinstall the client(ssms) to solve the issue
Thanks
I got the same error for SQL 2016 and the only solution for me was to completely uninstall (in appwiz.cpl) all entries Visual Studio + SQL. Then I executed VisualStudioUninstaller and I reinstalled SQL and after Visual Studio.
I got the same message when I try to alter table to add new column . Issue is I haven't enclosed data types using '[' datatype ']' . Its Real data type. But It got fixed when I enclosed in using square braces.
My solution for this was to extract the csv file with the python library pandas rather than saving it with Excel. Further, I removed columns that I didn't need as a few columns caused formatting errors during the import.
For those who are unable to modify their SQL configuration due to administrative restrictions at work:
I was able to open a new query page by creating a SQL Server Scripts project and adding a new query through the solution explorer
I got the message after deleting a database. The database was gone, so I moved on.

Table valued function only returns CLR error

I have read-only access to a database that was set up for a third-party, closed-source app. Once group of (hopefully) useful table functions only returns the error:
Failed to initialize the Common
Language Runtime (CLR) v2.0.50727 with
HRESULT 0x80131522. You need to
restart SQL server to use CLR
integration features. (severity 16)
But in theory, the third-party app should be able to use the function (either directly or indirectly), so I'm convinced I'm not setting things up right.
I'm very new to SQL Server, so I could be missing something obvious. Or I could be missing something really slight, I have no idea.
Here is an example of a query that returns the above error:
SELECT * FROM
dbo.UncompressDataDateRange(4,'Apr 24 2010 12:00AM','Apr 30 2010 12:00AM')
Where the function takes three parameters:
The Data Set (int) -- basically the data has 6 classifications, and the giant table this should be pulling from has a column to indicate which is which.
startDate (smalldatetime)
endDate (smalldatetime)
There are other, similar functions that expand on the same idea, all returning the same error.
Quick Note:
I'm not sure if this is relevant, but I was able to connect to the database via SQL Studio (but without the privs to script the functions as code), and a checked the dependency for the above sample function. It turns out that it is a dependent of a view that I have gotten to work, and that view is dependent of the larger, much-hairier data-table.
This makes me think I should somehow be pointing the function at the results of the view, but I'm not seeing any documentation that shows how that is done.
The error message appears to hint that:
You need to restart SQL server to use CLR integration features
Perhaps the 3rd party installer enabled CLR integration, but was not able to restart SQL Server.

Resources