{"id":1338,"date":"2012-03-09T04:15:48","date_gmt":"2012-03-09T09:15:48","guid":{"rendered":"http:\/\/www.dpriver.com\/blog\/?page_id=1338"},"modified":"2012-03-09T04:15:48","modified_gmt":"2012-03-09T09:15:48","slug":"sorting-views-script-by-dependency","status":"publish","type":"page","link":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/sorting-views-script-by-dependency\/","title":{"rendered":"Sorting Views Script by Dependency"},"content":{"rendered":"

If you scripted the views for a database in Microsoft SQL Server and for some reason, the order of “create views” does not consider the dependencies of views with each other. Just try to imagine you have a hundred views to sort according to dependency and you have to figure it out manually. A software tool for this will be very handy.<\/p>\n

    \r\nExample:\r\n\r\n----------------------------------\r\nUSE dbSAS\r\n\r\n\r\nCREATE VIEW [dbo.vwGrades]\r\nAS\r\nSELECT s.id, s.name, ssub.subjectname, g.grade from dbo.vwStudents as s\r\ninner join dbo.vwSubjects as ssub on s.id = ssub.id inner join grades as g on ssub.id = g.id\r\norder by s.name asc\r\n\r\nGO\r\n\r\n\r\nCREATE VIEW [dbo.vwSubjects]\r\nAS\r\nSELECT s.id, s.name, ssub.id, ssub.subjectname from dbo.vwStudents as s inner join\r\nstudent_subjects as ssub on s.id = ssub.id order by s.name asc\r\n\r\n\r\nGO\r\n\r\nCREATE VIEW [dbo.vwStudents]\r\nAS\r\nSELECT id, first + ' ' + last as name from students;\r\n\r\nGO\r\n<\/pre>\nExecuting this script in SQL Server 2005 will cause some error because the first view refers to views vwStudents and vwSubjects which are not created yet.<\/p>\nWe have created a tool based on General SQL Parser<\/a> to sort view script by dependency automatically for you.<\/p>\nDownload this demo: C# version<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"If you scripted the views for a database in Microsoft SQL Server and for some reason, the order of “create views” does not consider the dependencies of views with each other. Just try to imagine you have a hundred views to sort according to dependency and you have to figure it out manually. A software tool for this will be very handy. Example: ———————————- USE dbSAS CREATE VIEW [dbo.vwGrades] AS SELECT s.id, s.name, ssub.subjectname, g.grade from dbo.vwStudents as s inner join dbo.vwSubjects as ssub on s.id = ssub.id inner join grades as g on ssub.id = g.id order by s.name\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":235,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"gsp_feature_page_tt.php","meta":[],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":5}},"yoast_head":"\nHelp you to make better use of General SQL Parser<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\t\n