While working in support projects for sometime I often realized that it is good to have results of a SQL Query as output in a file for later reference. So, if you do a data fix today and someone later asks you to confirm that the issue was infact fixed by you then you can show them the output file with SQL query output and tell them - "Here it is..."
So now as we have defined one of the purpose of getting SQL query output into a file lets show you how to do the same.
Lets say there is an Employee table with the following structure -
create table Employee
(
EmpId int,
FirstName varchar(100),
LastName varchar(100),
Age int,
Salary float,
ManagerId int,
DeptId int
)
go
Now lets say this table is having around 10,000 records and recently our Department 'Accounts' with DeptId as 1 has a new manager 'TOM' with ManagerId as 2. So we try to update the table Employee using the following query -
update table Employee
set ManagerId = 2
where
DeptId = 1
go
Now after running this script we check our data fix and write the following query -
select * from Employee where DeptId = 1
go
After running this SQL query we see that the output is as expected and the new manager 'TOM' has been set correctly for Department 'Accounts' but we do want to keep a proof of this and so we open the isql in the command prompt (for Windows) and write the following to get the output of this result directly to the file, so we write the following command -
isql -S<<SERVER NAME>> -D<<DATABASE NAME>> -U<<LOGIN NAME>> -P<<PASSWORD>> -i<<INPUT SQL FILE PATH>> -o<<OUTPUT RESULT FILE PATH>> -w1000 -n
and we are done with it. The above statement must be written in the command prompt or UNIX prompt and not within an isql session. Also, in the above statement remove the <<>> markers to execute the statement with actual parameters.
No comments:
Post a Comment