|
1 | 1 | <?php |
2 | | -$serverName = "tcp:yourserver.database.windows.net,1433"; |
| 2 | +$serverName = 'tcp:yourserver.database.windows.net,1433'; |
3 | 3 | $connectionOptions = [ |
4 | | - "Database" => "yourdatabase", |
5 | | - "Uid" => "yourusername", |
6 | | - "PWD" => "yourpassword", |
| 4 | + 'Database' => 'yourdatabase', |
| 5 | + 'Uid' => 'yourusername', |
| 6 | + 'PWD' => 'yourpassword', |
7 | 7 | ]; |
8 | 8 | // Establishes the connection |
9 | 9 | $conn = sqlsrv_connect($serverName, $connectionOptions); |
|
12 | 12 | * Stored Procedure |
13 | 13 | */ |
14 | 14 |
|
15 | | -$tsql = "CREATE PROCEDURE sp_GetCompanies22 AS BEGIN SELECT [CompanyName] FROM SalesLT.Customer END"; |
| 15 | +$tsql = 'CREATE PROCEDURE sp_GetCompanies22 AS BEGIN SELECT [CompanyName] FROM SalesLT.Customer END'; |
16 | 16 | $storedProc = sqlsrv_query($conn, $tsql); |
17 | 17 | if ($storedProc == false) { |
18 | | - echo "Error creating Stored Procedure"; |
| 18 | + echo 'Error creating Stored Procedure'; |
19 | 19 | die(formatErrors(sqlsrv_errors())); |
20 | 20 | } |
21 | 21 | sqlsrv_free_stmt($storedProc); |
22 | 22 |
|
23 | | -$tsql = "exec sp_GETCompanies22"; |
| 23 | +$tsql = 'exec sp_GETCompanies22'; |
24 | 24 | // Executes the query |
25 | 25 | $getProducts = sqlsrv_query($conn, $tsql); |
26 | 26 | // Error handling |
27 | 27 | if ($getProducts == false) { |
28 | | - echo "Error executing Stored Procedure"; |
| 28 | + echo 'Error executing Stored Procedure'; |
29 | 29 | die(formatErrors(sqlsrv_errors())); |
30 | 30 | } |
31 | 31 | $productCount = 0; |
|
40 | 40 | } |
41 | 41 | $ctr++; |
42 | 42 | echo($row['CompanyName']); |
43 | | - echo("<br/>"); |
| 43 | + echo('<br/>'); |
44 | 44 | $productCount++; |
45 | 45 | } |
46 | 46 | sqlsrv_free_stmt($getProducts); |
47 | | -$tsql = "DROP PROCEDURE sp_GETCompanies22"; |
| 47 | +$tsql = 'DROP PROCEDURE sp_GETCompanies22'; |
48 | 48 |
|
49 | 49 | $storedProc = sqlsrv_query($conn, $tsql); |
50 | 50 | if ($storedProc == false) { |
51 | | - echo "Error dropping Stored Procedure"; |
| 51 | + echo 'Error dropping Stored Procedure'; |
52 | 52 | die(formatErrors(sqlsrv_errors())); |
53 | 53 | } |
54 | 54 | sqlsrv_free_stmt($storedProc); |
|
59 | 59 | */ |
60 | 60 |
|
61 | 61 | if (sqlsrv_begin_transaction($conn) == false) { |
62 | | - echo "Error opening connection"; |
| 62 | + echo 'Error opening connection'; |
63 | 63 | die(formatErrors(sqlsrv_errors())); |
64 | 64 | } |
65 | 65 |
|
66 | 66 | /* Set up and execute the first query. */ |
67 | | -$tsql1 = "INSERT INTO SalesLT.SalesOrderDetail |
| 67 | +$tsql1 = 'INSERT INTO SalesLT.SalesOrderDetail |
68 | 68 | (SalesOrderID,OrderQty,ProductID,UnitPrice) |
69 | | - VALUES (71774, 22, 709, 33)"; |
| 69 | + VALUES (71774, 22, 709, 33)'; |
70 | 70 | $stmt1 = sqlsrv_query($conn, $tsql1); |
71 | 71 |
|
72 | 72 | /* Set up and execute the second query. */ |
73 | | -$tsql2 = "UPDATE SalesLT.SalesOrderDetail SET OrderQty = (OrderQty + 1) WHERE ProductID = 709"; |
| 73 | +$tsql2 = 'UPDATE SalesLT.SalesOrderDetail SET OrderQty = (OrderQty + 1) WHERE ProductID = 709'; |
74 | 74 | $stmt2 = sqlsrv_query($conn, $tsql2); |
75 | 75 |
|
76 | 76 | /* If both queries were successful, commit the transaction. */ |
|
100 | 100 | $getProducts = sqlsrv_query($conn, $tsql1); |
101 | 101 | // Error handling |
102 | 102 | if ($getProducts == false) { |
103 | | - echo "Error deleting the UDF"; |
| 103 | + echo 'Error deleting the UDF'; |
104 | 104 | die(formatErrors(sqlsrv_errors())); |
105 | 105 | } |
106 | 106 | $tsql1 = 'CREATE FUNCTION dbo.ifGetTotalItems (@OrderID INT) RETURNS TABLE WITH SCHEMABINDING AS RETURN ( |
|
111 | 111 | $getProducts = sqlsrv_query($conn, $tsql1); |
112 | 112 | // Error handling |
113 | 113 | if ($getProducts == false) { |
114 | | - echo "Error creating the UDF"; |
| 114 | + echo 'Error creating the UDF'; |
115 | 115 | die(formatErrors(sqlsrv_errors())); |
116 | 116 | } |
117 | | -$tsql1 = "SELECT s.SalesOrderID, s.OrderDate, s.CustomerID, f.TotalItems |
| 117 | +$tsql1 = 'SELECT s.SalesOrderID, s.OrderDate, s.CustomerID, f.TotalItems |
118 | 118 | FROM SalesLT.SalesOrderHeader s |
119 | 119 | CROSS APPLY dbo.ifGetTotalItems(s.SalesOrderID) f |
120 | | -ORDER BY SalesOrderID;"; |
| 120 | +ORDER BY SalesOrderID;'; |
121 | 121 | $getProducts = sqlsrv_query($conn, $tsql1); |
122 | 122 | // Error handling |
123 | 123 | if ($getProducts == false) { |
124 | | - echo "Error executing the UDF"; |
| 124 | + echo 'Error executing the UDF'; |
125 | 125 | die(formatErrors(sqlsrv_errors())); |
126 | 126 | } |
127 | 127 | $productCount = 0; |
128 | 128 | $ctr = 0; |
129 | 129 | ?> |
130 | 130 | <h1> First 10 results are after executing a query that uses the UDF: </h1> |
131 | 131 | <?php |
132 | | -echo "SalesOrderID CustomerID TotalItems"; |
133 | | -echo("<br/>"); |
| 132 | +echo 'SalesOrderID CustomerID TotalItems'; |
| 133 | +echo('<br/>'); |
134 | 134 |
|
135 | 135 | while ($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC)) { |
136 | 136 | // Printing only the top 10 results |
|
142 | 142 | ' ', |
143 | 143 | 11 |
144 | 144 | ) . $row['TotalItems']; |
145 | | - echo("<br/>"); |
| 145 | + echo('<br/>'); |
146 | 146 | $productCount++; |
147 | 147 | } |
148 | 148 | sqlsrv_free_stmt($getProducts); |
0 commit comments