{"id":8648,"date":"2018-04-24T05:55:15","date_gmt":"2018-04-24T05:55:15","guid":{"rendered":"https:\/\/assignment.essayshark.com\/blog\/?p=8648"},"modified":"2022-01-05T11:05:05","modified_gmt":"2022-01-05T11:05:05","slug":"dynamic-sql-query-cursors-and-exceptions","status":"publish","type":"post","link":"https:\/\/assignmentshark.com\/blog\/dynamic-sql-query-cursors-and-exceptions\/","title":{"rendered":"Dynamic SQL Query, Cursors, and Exceptions"},"content":{"rendered":"<blockquote><p><i><span style=\"font-weight: 400;\">What is <\/span><\/i><i>dynamic SQL query<\/i><i><span style=\"font-weight: 400;\"> and how do you use it in your assignment? Everyone can find an answer to this question in our sample. In one of your projects using dynamic SQL you can solve the tasks of constructing dynamic reports, while in others \u2013 data migration. Also, dynamic SQL is indispensable in cases where you want to create or change, or retrieve data or objects, but the values or names come as parameters. If it seems too difficult for you, you may leave your assignment to AssignmentShark.com <a href=\"https:\/\/assignmentshark.com\/\" target=\"_blank\" rel=\"noopener\">assignment help online service<\/a>.<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">On our blog, we show different examples of how different problems can be solved by our experts. You can use our service for handling homework in different disciplines. With the use of our service, you can get high grades. Our experts use different methods for completing different orders. You can see this by looking through examples on our blog. As you can see from the sample below, we can deal with tasks of any difficulty. If you have a similar task, look at what result you may get!<\/span><\/i><\/p><\/blockquote>\n<p><!--more--><\/p>\n<h2 style=\"text-align: center;\"><b>Dynamic SQL Queries, Cursors, and Exceptions<\/b><b><br \/>\n<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">We need to develop a stored procedure that displays the names of all user tables of the database of the individual subject area in alphabetical order, using the cursor-based system view sysobjects.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here is an SQL code to implement it:<\/span><\/p>\n<p>[code language=&#8221;sql&#8221;]<br \/>\nCREATE PROCEDURE ShowTables<br \/>\nAS<br \/>\nBEGIN<br \/>\n\t&#8211;VARIABLE FOR WORK @name<br \/>\n\tDECLARE @name VARCHAR(100)<\/p>\n<p>\t&#8211;CREATE CURSOR<br \/>\n\tDECLARE db_cursor CURSOR FOR<br \/>\n\tSELECT name<br \/>\n\tFROM sysobjects<br \/>\n\tWHERE TYPE = &#8216;U&#8217;<br \/>\n\tORDER BY name <\/p>\n<p>\t&#8211;OPEN CURSOR<br \/>\n\tOPEN db_cursor<\/p>\n<p>\t&#8211;THE SAMPLE DATA OF THE FIRST LINE<br \/>\n\tFETCH NEXT FROM db_cursor INTO @name<\/p>\n<p>\t&#8211;WHILE THERE IS DATA IN THE CURSOR &#8211; THE WHOLE SAMPLE<br \/>\n\tWHILE @@FETCH_STATUS = 0<br \/>\n\tBEGIN<br \/>\n\t\tPRINT @name<br \/>\n\t\tFETCH NEXT FROM db_cursor INTO @name<br \/>\n\tEND<\/p>\n<p>\t&#8211;CLOSE THE CURSOR<br \/>\n\tCLOSE db_cursor<\/p>\n<p>\t&#8211;DESTROY THE CURSOR<br \/>\n\tDEALLOCATE db_cursor<br \/>\nEND<\/p>\n<p>&#8211;OVERVIEW PROCEDURE<br \/>\nEXEC ShowTables<\/p>\n<p>[\/code]<\/p>\n<p>And here is the result we obtain:<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-8658\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_1-300x270.png\" alt=\"\" width=\"195\" height=\"176\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_1-300x270.png 300w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_1-768x692.png 768w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_1-1024x923.png 1024w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_1.png 1053w\" sizes=\"auto, (max-width: 195px) 100vw, 195px\" \/><\/a><\/p>\n<p>Now we need to modify the last exercise in SQL code to display the names of all user tables of the database of individual subject areas as well as the number of records in each table in alphabetical order, using the cursor based on the system view sysobjects and grouping query inside the dynamic SQL.<\/p>\n<p>[code language=&#8221;sql&#8221;]<br \/>\nCREATE PROCEDURE ShowTables2<br \/>\nAS<br \/>\nBEGIN<br \/>\n\t&#8211;VARIABLE FOR WORK @name<br \/>\n\tDECLARE @name VARCHAR(100)<\/p>\n<p>\t&#8211;CREATE CURSOR<br \/>\n\tDECLARE db_cursor CURSOR FOR<br \/>\n\tSELECT name<br \/>\n\tFROM sysobjects<br \/>\n\tWHERE TYPE = &#8216;U&#8217;<br \/>\n\tORDER BY name <\/p>\n<p>\t&#8211;OPEN CURSOR<br \/>\n\tOPEN db_cursor<\/p>\n<p>\t&#8211;THE SAMPLE DATA OF THE FIRST LINE<br \/>\n\tFETCH NEXT FROM db_cursor INTO @name<\/p>\n<p>\t&#8211;WHILE THERE IS DATA IN THE CURSOR &#8211; THE WHOLE SAMPLE<br \/>\n\tWHILE @@FETCH_STATUS = 0<br \/>\n\tBEGIN<br \/>\n\t\tDECLARE @command NVARCHAR(max)<br \/>\n\t\tDECLARE @cnt NVARCHAR(max)<br \/>\n\t\tSET @command = &#8216;SELECT @cntOUT=COUNT(*) FROM [&#8216;+@name+&#8217;]&#8217;<br \/>\n\t\tSET @cnt = 0<br \/>\n\t\tEXEC sp_executesql @command, N&#8217;@cntOUT INT OUTPUT&#8217;, @cntOUT=@cnt OUTPUT<br \/>\n\t\tPRINT &#8216;Table &#8216;+ @name +&#8217; &#8216;+@cnt<br \/>\n\t\tFETCH NEXT FROM db_cursor INTO @name<br \/>\n\tEND<\/p>\n<p>\t&#8211;CLOSE THE CURSOR<br \/>\n\tCLOSE db_cursor<\/p>\n<p>\t&#8211;DESTROY THE CURSOR<br \/>\n\tDEALLOCATE db_cursor<br \/>\nEND<\/p>\n<p>&#8211;OVERVIEW PROCEDURE<br \/>\nEXEC ShowTables2<\/p>\n<p>[\/code]<\/p>\n<p><span style=\"font-weight: 400;\">Here is the result:<\/span><\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-8656\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_2-300x201.png\" alt=\"\" width=\"213\" height=\"143\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_2-300x201.png 300w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_2-768x514.png 768w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_2-1024x685.png 1024w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_2.png 1222w\" sizes=\"auto, (max-width: 213px) 100vw, 213px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">And the last thing we need to do is write a procedure that would take the error code and fix it in the special table.<\/span><\/p>\n<p>[code language=&#8221;sql&#8221;]<br \/>\nCREATE PROCEDURE Errors<br \/>\nAS<br \/>\nBEGIN<br \/>\n\tIF NOT EXISTS (SELECT * FROM sysobjects WHERE name = &#8216;tbl_Exception&#8217; AND type = &#8216;U&#8217;)<br \/>\n\t\tCREATE TABLE tbl_Exception (<br \/>\n\t\tId NUMERIC(10,0) NOT NULL PRIMARY KEY IDENTITY(1,1),<br \/>\n\t\tErrorDate DATETIME NOT NULL DEFAULT GETDATE(),<br \/>\n\t\tServerName NVARCHAR(max) NOT NULL DEFAULT @@SERVERNAME,<br \/>\n\t\tDataBaseName NVARCHAR(max) NOT NULL DEFAULT ORIGINAL_DB_NAME(),<br \/>\n\t\tCurrentLogin NVARCHAR(max) NOT NULL DEFAULT SYSTEM_USER,<br \/>\n\t\tUserName NVARCHAR(max) NOT NULL DEFAULT USER_NAME(),<br \/>\n\t\tUsing NVARCHAR(max) NOT NULL DEFAULT APP_NAME(),<br \/>\n\t\tErrorCode INT NOT NULL,<br \/>\n\t\tErrorDescription NVARCHAR(max) NOT NULL)<\/p>\n<p>\t&#8211;DECLARE VARIABLES USED IN ERROR CHECKING<br \/>\n\tDECLARE @ErrorNum INT<br \/>\n\tDECLARE @ErrorMessage NVARCHAR(max)<\/p>\n<p>\t&#8211;SAVE @@ErrorNum VALUE IN FIRST LOCAL VARIABLE<br \/>\n\tSET @ErrorNum = ERROR_NUMBER()<\/p>\n<p>\t&#8211;SAVE @@ErrorMessage VALUE IN SECOND LOCAL VARIABLE<br \/>\n\tSET @ErrorMessage = ERROR_MESSAGE()<\/p>\n<p>\t&#8211;IF SECOND TEST VARIABLE CONTAINS NON-ZERO, OVERWRITE VALUE IN SECOND LOCAL VARIABLE<br \/>\n\tIF (@ErrorNum &lt;&gt; 0)<br \/>\n\t\tINSERT INTO tbl_Exception(ErrorCode, ErrorDescription) VALUES (@ErrorNum, @ErrorMessage)<br \/>\nEND<\/p>\n<p>&#8211;CHECK<br \/>\nBEGIN TRY<br \/>\n\tSELECT 1\/0<br \/>\nEND TRY<\/p>\n<p>BEGIN CATCH<br \/>\n\tEXEC Errors<br \/>\nEND CATCH<\/p>\n<p>&#8211;OVERVIEW<br \/>\nSELECT * FROM tbl_Exception<\/p>\n<p>[\/code]<\/p>\n<p><span style=\"font-weight: 400;\">And here is the result:<\/span><\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-8654 size-large\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_3-1024x59.png\" alt=\"\" width=\"604\" height=\"35\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_3-1024x59.png 1024w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_3-300x17.png 300w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_3-768x44.png 768w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2018\/04\/query-sql_3.png 1600w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is dynamic SQL query and how do you use it in your assignment? Everyone can find an answer to this question in our sample. In one of your projects using dynamic SQL you can solve the tasks of constructing dynamic reports, while in others \u2013 data migration. Also, dynamic SQL is indispensable in cases [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,35],"tags":[],"class_list":["post-8648","post","type-post","status-publish","format-standard","hentry","category-it","category-samples"],"_links":{"self":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/8648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/comments?post=8648"}],"version-history":[{"count":11,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/8648\/revisions"}],"predecessor-version":[{"id":12863,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/8648\/revisions\/12863"}],"wp:attachment":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/media?parent=8648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/categories?post=8648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/tags?post=8648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}