51

Context: I'm having difficulty modifying a stored procedure in SQL Server 2016. The stored procedure performs parsing of json data within a file. For some reason I'm able to execute the stored procedure and it executes successfully but when I try to modify the stored procedure I get the following message:

enter image description here

Question: Does anyone have any troubleshooting tips? Below is the content of the stored procedure. SQL Server 2016 supports the various functions used including the OPENJSON function.

USE mattermark_sandbox
GO

CREATE PROCEDURE get_company_data 

AS

IF OBJECT_ID('tempdb..##jsondump') IS NOT NULL DROP TABLE ##jsondump
IF OBJECT_ID('tempdb..##jsonparsed') IS NOT NULL DROP TABLE ##jsonparsed
IF OBJECT_ID('tempdb..##json_loop') IS NOT NULL DROP TABLE ##json_loop

CREATE TABLE ##jsondump (
    [my_json] [nvarchar](max) NULL
) 

-- Create a table to house the parsed content
CREATE TABLE ##jsonparsed (
    [id] [int] NULL,
    [url] [varchar](255) NULL,
    [company_name] [varchar](255) NULL,
    [domain] [varchar](255) NULL
)

-- Clear ##jsondump
TRUNCATE TABLE ##jsondump;

-- Clear ##jsonparsed ( only if you don't want to keep what's already there )
TRUNCATE TABLE ##jsonparsed;

-- Import ( single column ) JSON
--IMPORTANT: Need to be sure the company_data.json file actually exists on the remote server in that directory 
BULK INSERT ##jsondump
FROM 'C:\mattermark_etl_project\company_data.json' -- ( <-- my file, point to your own )
WITH (
    ROWTERMINATOR = '\n'
);

-- Select JSON into ##jsonparsed
SELECT my_json 
INTO ##json_loop
FROM ##jsondump;

--SELECT * FROM ##jsondump;

INSERT INTO ##jsonparsed (
    id, [url], company_name, domain
)
SELECT DISTINCT
    jsn.id, jsn.[url], jsn.company_name, jsn.domain
FROM ##json_loop
OUTER APPLY (

    SELECT * FROM OPENJSON(##json_loop.my_json, '$.companies' )
    WITH (
        id INT '$.id',
        [url] VARCHAR(255) '$.url',
        company_name VARCHAR(255) '$.company_name',
        domain VARCHAR(255) '$.domain'
    )

) AS jsn

DECLARE @bcp_cmd4 VARCHAR(1000);
DECLARE @exe_path4 VARCHAR(200) = 
    ' cd C:\Program Files\Microsoft SQL Server\100\Tools\Binn\ & ';
SET @bcp_cmd4 =  @exe_path4 + 
    ' BCP.EXE "SELECT ''Company_ID'', ''MatterMark_URL'', ''Company_Name'', ''Domain'' UNION ALL SELECT DISTINCT cast(id as varchar( 12 )) as id, url, company_name, domain FROM ##jsonparsed" queryout ' +
    ' "C:\mattermark_etl_project\company_data.txt" -T -c -q -t0x7c -r\n';
PRINT @bcp_cmd4;
EXEC master..xp_cmdshell @bcp_cmd4,no_output;

SELECT DISTINCT * FROM ##jsonparsed
ORDER BY id ASC;

DROP TABLE ##jsondump 
DROP TABLE ##jsonparsed 
DROP TABLE ##json_loop 

/*
-- To allow advanced options to be changed.  
EXEC sp_configure 'show advanced options', 1;  
GO  

-- To update the currently configured value for advanced options.  
RECONFIGURE;  
GO  

-- To enable the feature.  
EXEC sp_configure 'xp_cmdshell', 1;  
GO  

-- To update the currently configured value for this feature.  
RECONFIGURE;  
GO  
*/

exec xp_cmdshell 'C:\mattermark_etl_project\powershell "C:\mattermark_etl_project\open_file.ps1"',no_output

emalcolmb
  • 1,585
  • 4
  • 18
  • 43
  • 1
    At what stage in the operation are you receiving this error? When you hit execute? – Dale K Jul 30 '19 at 00:28
  • Your code doesn't show anything being modified? Its showing a create statement? – Dale K Jul 30 '19 at 00:30
  • 4
    Are you 100% sure that your SQL Server, and SSMS versions are both 2016? SSMS allows you to connect to any database version. The error would imply that the script includes unsupported features in either database engine or management studio. Try `select @@version` – Dale K Jul 30 '19 at 00:40
  • 9
    Seems like you might be trying to talk to a SQL Server 2019 database using SSMS 2016. – Dale K Jul 30 '19 at 00:57
  • Turns out I'm actually running SQL Server 2019 CTP 2.5 which should support the features needed in the proc. I'm usingf SSMS v18.0. – emalcolmb Jul 30 '19 at 01:25
  • 2
    Yeah, but what will be happening is that for some functions SSMS doesn't have the capabilities to properly interact with the database engine because its a newer version. If you upgrade SSMS to the same version all should be well. – Dale K Jul 30 '19 at 01:29
  • 3
    Using SSMS version 18.0 instead of 17.0 seems to be working. – emalcolmb Jul 30 '19 at 01:32

4 Answers4

46

Using SSMS version 18.0 instead of 17.0 seems to be working.

You can download it from https://learn.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-2017

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
emalcolmb
  • 1,585
  • 4
  • 18
  • 43
  • 18
    Unfortunately SSMS 18 doesn't support Debugging T-SQL, and it's a huge pain to install VS to use SSDT to debug sprocs (and many people report SSDT currently hangs when debugging T-SQL on SQL Server 2019, _joy_). – Dai Jan 02 '20 at 21:38
  • 2
    Everybody, please sign this user voice related item here to help its way back to SSMS: https://feedback.azure.com/forums/908035-sql-server/suggestions/35881492-put-debugger-back-into-ssms-18 – hamid reza May 18 '20 at 06:28
  • 22
    Why the F should I need SSMS 18.0 to look at the definition of a proc? Makes no sense. As per Dai, lack of debugging is just junk. So frustrating. – Shiv Aug 17 '20 at 03:11
  • 2
    Agreed! @shiv – emalcolmb Aug 17 '20 at 15:08
  • 1
    @hamidreza looks like the user voice link is broken, can you post an updated link? (Or an update on the situation if it's solved? -- I would love to vote on it if it's not though.) – BrainSlugs83 Jun 23 '22 at 16:07
18

You can use a query to view the stored procedure definition without installing a new SSMS. Before running this query, you can optionally use the SSMS menu item Query -> Results To -> Results to Text to format the result in one text field.

exec sp_helptext [get_company_data]

(Where get_company_data is the stored procedure name.)

Also note that the stored procedure 'Modify' option just opens a regular query tab pre-filled with the definition as an ALTER PROCEDURE, you can instead update it by running an ALTER PROCEDURE in a normal query tab.

Taylor G
  • 846
  • 9
  • 13
3

There is a workaround for this issue: Instead of selecting Modify, right klick on the database name and select Tasks> Generate Scripts: Select Specific Objects, Check your object.

0

If you are using the Oracle server. Then use OPENQUERY. Select Syntax as follows:

  select * from OPENQUERY(LINK_SERVER_NAME,'select * from DUAL')