I created a stored procedure using dynamic SQL and a cursor a while ago. For upkeeping and adjusting purposes, I'm starting to think that standard SQL would be better than having to adjust the cursor. However, to assess this option, I really don't want to go through rewritting all this logic.
Is there a way to see what queries my cursor creates? I realize there is SQL Profiler (which I used very little), however, after a quick search, I saw that it'll simply display "exec my_stored_procedure" instead of showing the entire script.
Are there any ideas?
My SQL Server version: Microsoft SQL Server 2017 (RTM-CU22) (KB4577467) - 14.0.3356.20 (X64) Aug 20 2020 22:33:27 Copyright (C) 2017 Microsoft Corporation Developer Edition (64-bit) on Windows Server 2019 Standard 10.0 (Build 17763: ) (Hypervisor)
Thank you
If your dynamic SQL is built in a variable before being executed, you could just use a PRINT statement to see what the queries are, like PRINT #SQL. Then it would show in your Messages window in SSMS.
Related
This seems like something so simple but it may not even be possible. I'm very new to SQL but have developed a report that is exporting to an excel spreadsheet. Is there coding that can actually open the excel spreadsheet? I don't mean like link to it or import it. I mean literally open it instead of me fumbling through folders. The coding is in Oracle SQL Developer.
Here's a script in SQLcl that dumps the query results to a csv then opens it.
the same script will work in the SQL Developer worksheet.
The script
turns off headers and feedback.
sets the format of the results to CSV
Spools the results to "users.csv"
Stops spooling
Issues "host open users.csv" which tells the O/S to open the file which should be excel unless the os has csv associated to another application
sql klrice/klrice
SQLcl: Release 18.1 Production on Wed Feb 07 17:28:32 2018
Copyright (c) 1982, 2018, Oracle. All rights reserved.
Last Successful login time: Wed Feb 07 2018 17:28:56 -05:00
Connected to:
Oracle Database 12c Standard Edition Release 12.1.0.2.0 - 64bit Production
SQL> set hea off
SQL> set feed off
SQL> set sqlformat csv
SQL> spool users.csv
SQL> select * from user_objects;
...rows.....
SQL> spool off
SQL> host open users.csv
As the title says, I need to restore a SQL Server 2012 database (express) to a SQL Server 2008 R2 production database.
I cannot find a way to do so.
The scripting seems to fail due to the size of one of the tables having about 300.000 records.
Any way to "downgrade" the 2012 database?
Thanks
Michael
You CANNOT do this - you cannot attach/detach or backup/restore a database from a newer version of SQL Server (2012) down to an older version (2008 R2) - the internal file structures are just too different to support backwards compatibility. There's no way, no trick, no hack, no magic to make this happen. Period.
You can either get around this problem by
using the same version of SQL Server on all your machines - then you can easily backup/restore databases between instances
otherwise you can create the database scripts for both structure (tables, view, stored procedures etc.) and for contents (the actual data contained in the tables) either in SQL Server Management Studio (Tasks > Generate Scripts) or using a third-party tool
or you can use a third-party tool like Red-Gate's SQL Compare and SQL Data Compare to do "diffing" between your source and target, generate update scripts from those differences, and then execute those scripts on the target platform; this works across different SQL Server versions.
When scripting a SQL Server 2000 database, on an SQL Server 2000 version of SQL Server, with SQL Server Management Studio 2008 R2 (10.50.1617.0) i get the error:
Creating a user without an associated login is not supported in SQL Server 2008 R2.
With the full stack trace:
Microsoft.SqlServer.Management.Smo.SmoException: Creating a user without an associated login is not supported in SQL Server 2008 R2.;
at Microsoft.SqlServer.Management.SqlScriptPublish.GeneratePublishPage.worker_DoWork(Object sender, DoWorkEventArgs e) at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
What is a good way to resolve this issue.
i've considered:
creating a login (and incur the wrath high atop the thing)
deleting the user (and incur the wrath from high atop the place)
select some rather than all objects to script, and don't script the user that offends SQL Server 2008 R2
But i'll let people on SO post answers, get answers upvoted, and accept an answer that best solves the problem.
As instructed. Just don't script the database users that have no associated login. You can do this in the Tasks > Generate Scripts wizard (pointing 2008 or later SSMS at your 2000 instance) by choosing to select specific database objects and unchecking any troublesome users:
I suspect that your SQL Server 2000 database has user aliases: these were required in SQL Server 6.5 in some circumstances because it was, er, crap,
Note what MSDN says:
sp_addalias is provided for backward compatibility. Microsoft® SQL Server™ version 7.0 provides roles and the ability to grant permissions to roles as an alternative to using aliases.
Run sp_helpuser on the SQL Server 2000 box and review the output and remove them
I tried to execute scripts from [1] in model database and user-defined databases but they give definitions/scripts of only user-defined (non-system) views, i.e. those that I anyway can easily get from GUI.
How can I see/script the definition/script of a system view in SQL Server 2008 R2?
[1]
Answers to question "System Views text in SQL Server 2005"
System Views text in SQL Server 2005
select object_definition(object_id('[sys].[server_permissions]'))
AS [processing-instruction(x)] FOR XML PATH('')
(The XML bit is just to prevent long definitions getting truncated when viewed in SSMS)
You can also stop the SQL Server instance. Copy the mssqlsystemresource file (on my system this is at C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\Binn\mssqlsystemresource.mdf and then re-attatch the copy under a new name).
In the reattached version it is easier to poke around and see the various definitions using normal SSMS functionality.
I am developing some stored procedures in SQL Server 2008.
Some of our customers are running SQL Server 2000.
Is there a way to check whether my queries are compatible without having to install SQL Server 2000? Maybe a transact sql tester?
Regards,
Michel
You could set the compatibility mode to 80 but it's not guaranteed
I would also suggest installing MSDE at least to validate your code and performance.$
Or simply install full SQL Server 2000 to develop with.