SSMS crashes when opening Design Query Editor - sql-server

is there a fix for the crash in SQL-Server 2005 Management Studio when you try to open a design-query-editor with a sql-statementent preceding USE database;?
For example:
USE DB1;
SELECT * FROM Table1;
If you mark SELECT * FROM Table1;, you can open the editor succesfully.
If you mark both lines, SSMS will crash with a "Microsoft SQL Server Management Studio has stopped working"-Dialog(with the option to debug or close SSMS) and some additional information on this problem f.e.:
Problem signature:
Problem Event Name: BEX
Application Name: SqlWb.exe
Application Version: 2005.90.5000.0
Application Timestamp: 4d02772b
Fault Module Name: vdt80.dll
Fault Module Version: 8.0.50727.1826
Fault Module Timestamp: 480d6e41
Exception Offset: 0032ae42
Exception Code: c0000409
Exception Data: 00000000
OS Version: 6.1.7601.2.1.0.16.7
SQL-Server version:
SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')
9.00.5000.00 SP4 Enterprise Edition (64-bit)
OS:
Windows Server 2008 R2 Standard(SP 1)
Edit: this problem also occurs if i try to open the designer with T-SQL Statement like:
IF EXISTS(
SELECT *
FROM ClosingDate
WHERE DATEDIFF(DAY, DATEADD(DAY, -1, GETDATE()). ClosingDate) = 0
) UPDATE Table2 SET
Column1 = 255
WHERE
Column1 = 1

sure, take out the use db statement and change the code to this:
select * from DB1.dbo.Table1
this syntax will allow you to select data from other databases on the same server in which you are not in the context of.

Related

PowerBuilder application got "SQL Error 16945 The cursor was not declared"

I have script using cursor embedded in Powerbuilder 6 source code, it's connecting to SQL Server 2008 R2 as below.
DECLARE cdept CURSOR FOR
SELECT de0070 AS dept_code,
de0080 AS dept_name,
(CASE WHEN de0010 = 'A' THEN '1' ELSE '0' END) AS active_status,
de0120 AS div_code
FROM APFLIB.JPMDEPP 
WHERE de0010 = 'A'
ORDER BY de0070
USING l_trans;
but when I run the application I got "SQL Error 16945 The cursor was not declared." after I checked with this script:
MessageBox("SQL error " + String(l_trans.SQLDBCode), l_trans.SQLErrText)
SQL Error 16945
I've tried to change CLR Integration in SQL server properties but still no luck. How can I fix this error?

The metadata could not be determined because the statement <SELECT Statement 1> is not compatible with the statement <SELECT Statement 2>

There are about 100 packages deployed to SSIS server. All the packages are the same. All of them had worked for a long time. However, on one certain day all of them started failing with the same error:
"Microsoft SQL Server Native Client 11.0" Hresult: 0x80004005
Description: "The metadata could not be determined because the
statement 'SELECT TOP 1
1 [TableName]
FROM
(
SELECT TOP 1
1 [Ta' is not compatible with the statement 'SELECT TOP 1
1 [TableName]
FROM sys.[tables]
WHERE 1 = 0'.
End Error Error: 2019-11-15 12:39:20.40 Code: 0xC020204A
Source: Description: Unable to retrieve column
information from the data source. Make sure your target table in the
database is available. End Error Error: 2019-11-15 12:39:20.42
Code: 0xC004706B Source: SSIS.Pipeline
Description: failed validation and returned validation
status "VS_ISBROKEN". End Error Error: 2019-11-15 12:39:20.42
Code: 0xC004700C Source: SSIS.Pipeline
Description: One or more component failed validation. End Error
Error: 2019-11-15 12:39:20.43 Code: 0xC0024107 Source: Description: There were errors during task validation.
End Error DTExec: The package execution returned DTSER_FAILURE (1).
Started: 12:39:19 PM Finished: 12:39:20 PM Elapsed: 1.234 seconds.
The package execution failed. The step failed.
The simplified version of the statement which fails validation looks like this:
IF
(
SELECT COUNT(*)
FROM sys.[tables]
WHERE [is_ms_shipped] = 0
AND modify_date > '10/24/2019 11:33:00 AM'
) > 0
BEGIN
SELECT TOP 1
1 [TableName]
FROM
(
SELECT TOP 1
1 [TableName]
FROM sys.[tables] AS T
WHERE T.[is_ms_shipped] = 0
AND T.modify_date > '10/24/2019 11:33:00 AM'
UNION ALL
SELECT TOP 1
1 [TableName]
FROM sys.views AS T
WHERE T.[is_ms_shipped] = 0
AND T.modify_date > '10/24/2019 11:33:00 AM'
) q
END;
ELSE
BEGIN
SELECT TOP 1
1 [TableName]
FROM sys.[tables]
WHERE 1 = 0;
END
Both selects return the same columns with the same data types.
I know that I can rewrite this statement so that no error is returned.
And I did this for a couple of packages and deployed them.
However, the problem is that the packages cannot be easily replaced due to internal reasons. I need to figure out what happened and why this error appeared all of a sudden for all the packages and to restore the conditions in which the packages worked.
On the day before the issue the following Windows updates were installed on the machine where SSIS servers is running:
2019-11 Cumulative Update for Windows Server 2016 for x64-based
Systems (KB4525236).
This included "Security updates for ... the
Microsoft JET Database Engine"
2019-12 Servicing Stack Update for Windows Server 2016 for x64-based
Systems (KB4520724).
Seemed to be nothing related to SQL.
What I did:
I uninstalled the first update (KB4525236). (The second update
(KB4520724) is un-uninstallable)
I checked the versions of the ODBC Driver and aligned them with the versions that are installed on another environment where this same
package still executed successfully.
ODBC Driver versions
Didn’t help.
There are the following platform versions that are used:
SSIS server – version 13.0.5101.9
Source Server (accessed via OLE DB Source – Native OLE DB\SQL Server
Native Client 11.0) – version 13.0.5492.2
Thanks in advance for any suggestions.

Compatibility mode in SQL Server 2014?

The application I am using is written by dinosaurs, using *= and =* operators in sql queries. These queries do joins on 10 or so tables, and I have about 500 such queries. And all I have time for is to upgrade the existing software as-is instead of writing any code. So, updating the query builder functions in all of them is not feasible.
Sadly, I am supposed to upgrade the application to use SQL server 2014, instead of 2005. And in 2014, the backward compatibility for a lot of operators is removed. So, I need to enable some kind of compatibility option in SQL server 2014, if it is available. But, I don't know if there is such a thing, and if it is, how to enable it.
Found a query to find the current compatibility setting. Posting it here, in case someone else needs it.
WITH compatversions AS (
SELECT 65 AS MajorVersion ,'SQL Server 6.5' AS ServerVersion
UNION
SELECT 70,'SQL Server 7.0'
UNION
SELECT 80 , 'SQL Server 2000'
UNION
SELECT 90 , 'SQL Server 2005'
UNION
SELECT 100 , 'SQL Server 2008/R2'
UNION
SELECT 110 , 'SQL Server 2012'
UNION
SELECT 120 , 'SQL Server 2014'
)
SELECT TOP 3 ServerVersion,MajorVersion
,ServerVersion + ' ('+ CONVERT(VARCHAR(3),MajorVersion) +')' AS DropDownFormat
FROM compatversions
WHERE MajorVersion IN (
SELECT TOP 3 MajorVersion
FROM compatversions
WHERE MajorVersion <= CONVERT(INT,CAST(##microsoftversion/ 0x1000000 AS VARCHAR(3)) + '0')
ORDER BY MajorVersion DESC)
ORDER BY MajorVersion ASC;
Apparently, SQL server 2014 supports up to 2008 compatibility.

Detect Sql Express From TSQL

I need a safe (i.e consistent, robust) way of detecting whether or not the sql server I'm accessing is Sql Express. I think I'd prefer to be able to do this from TSQL, since I already have a connection string and all the libraries I need to execute TSQL (this should help avoid issues with whether or not WMI is installed/running, the user has permissions to access the registry etc).
Does anyone know of a way to do this ?
Thanks.
PS: Basically I want to do this so I can monitor my database size against the 4gb limit and take action when I approach it... but if I'm running on a full Sql Server edition then I don't want the code to worry about it since there is no (practical) hard coded limit. I could put in a manual setting in my program, but it would be much nicer if the code just did the right thing automatically, hence the need to know if the server is the 'Express' edition or not.
Use
SELECT SERVERPROPERTY('EditionID')
or
SELECT SERVERPROPERTY('Edition')
to detect which version of SQLServer is running.
http://msdn.microsoft.com/en-us/library/ms174396.aspx
This will return 0 if it's not, and something non-zero (126 here) if it is:
select charindex('Express Edition',##version)
Example in a boolean context:
if (select charindex('Express Edition',##version)) > 0
begin
print 'Express edition!'
end
else
begin
print 'Not Express edition!'
end
The SERVERPROPERTY function was already mentioned in multiple other answers, but there's a better solution than checking if a string contains "Express Edition": the EngineEdition argument, which returns an int value.
Quote from the link:
EngineEdition
Database Engine edition of the instance of SQL Server installed on the
server.
1 = Personal or Desktop Engine (Not available in SQL Server 2005 and
later versions.)
2 = Standard (This is returned for Standard, Web, and Business
Intelligence.)
3 = Enterprise (This is returned for Evaluation, Developer, and both
Enterprise editions.)
4 = Express (This is returned for Express, Express with Tools and
Express with Advanced Services)
5 = SQL Database
6 - SQL Data Warehouse
Base data type: int
So you can check for Express Edition like this:
if SERVERPROPERTY('EngineEdition') = 4
begin
select 'Express'
end
else
begin
select 'not Express'
end
There are a number of ways:
EXEC sp_server_info 2
Or
SELECT ##version
Or
SELECT serverproperty('ProductVersion')
You Can Also do this:
DECLARE #ver nvarchar(128)
SET #ver = CAST(serverproperty('ProductVersion') AS nvarchar)
SET #ver = SUBSTRING(#ver, 1, CHARINDEX('.', #ver) - 1)
IF ( #ver = '8' )
SELECT 'SQL Server 2000'
ELSE IF ( #ver = '9' )
SELECT 'SQL Server 2005'
ELSE
SELECT 'Unsupported SQL Server Version'
More info at: http://blog.devstone.com/aaron/default,date,2006-12-15.aspx
You can use SERVERPROPERTY('Edition') which will return "Express Edition"
What happens in SQL Server Express if you don't monitor the size, but get to the limit? Are there warnings first? Events in the event log?
If so, then you might do better to allow SQL Server to issue the warnings, and then just listen to them. The same might well apply for a SQL Server Enterprise installation if a disk gets full.

Version Agnostic SQL Server Script/Statement that detects existence of a specific database

Due to the packaged nature of the release, a SQL Server script (well more of a statement) needs to be created that can execute correctly on SQL Server 7.0 thru 2008 which can essentially achieve this:
if exists(select * from sys.databases where name = 'Blah')
Reasons this is difficult:
SQL 7 'sys.databases' is not valid
SQL 2008 'sysdatabases' is not valid
I stupidly parsed out the version number using serverproperty, to allow an IF depending on the version:
if (select CONVERT(int,replace(CONVERT(char(3),serverproperty ('productversion')),'.',''))) >= 80
Then discovered serverproperty does not exist under SQL 7.
Note that the SQL can be remote from the install, so no futzing around on the local machine - reg entries/file versions etc is of any use.
SQL Server error handling (especially 7.0) is poor, or maybe I don't understand it well enough to make it do a kind of try/catch.
I am now getting problem blindness to this, so any pointers would be appreciated.
Thanks,
Gareth
Try
USE database
and test ##ERROR.
USE database
IF ##ERROR <> 0 GOTO ErrExit
logic ...
RETURN 0
ErrExit:
RETURN 1
(or RAISERROR, or ...)
Thanks G Mastros
This looks like it might yield a 100% solution. It is available under SQL 7.
I need to complete and test, but at first glance I think it will fly.
Here's the draft code FYI.
create table #dwch_temp
(
name sysname
,db_size nvarchar(13)
,owner sysname
,dbid smallint
,created nvarchar(11)
,status nvarchar(600)
,compatibility_level tinyint
)
go
insert into #dwch_temp
exec sp_helpdb
if exists(select name from #dwch_temp where name = 'DWCHServer')
-- run the code
drop table #dwch_temp
You could try a TRY... CATCH around a USE [DatabaseName].
I don't have access to a SQL 7 instance, but I encourage you to try:
sp_helpDB
I know this works on sql 2000 and sql 2005 to get a list of databases. I suspect it works on SQL 7, too.
sysdatabases is a remnant from the Sybase era and is still present in SQL 2008 (although deprecated). You can check for the existence of a database with a query like this:
IF EXISTS (SELECT 1 FROM master..sysdatabases where name = 'Blah')

Resources