Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Executable EXE file

Author  Topic 

patrickjao
Starting Member

24 Posts

Posted - 2012-07-09 : 03:15:55
How to create sqlView/sql query to exe file?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-09 : 07:13:20
Did you mean how to create an exe to run a set of SQL commands?

If that is so: normally people do not create an .exe for sql commands. They use the SQLCMD utility. SQLCMD allows you to execute SQL statements you have stored in a text file, or even SQL commands provided as parameters to the utility. http://msdn.microsoft.com/en-us/library/ms162773.aspx

If you do want to distribute an exe, you would need to create a .net (or some such) application. But that is less desirable for a variety of reasons if all you are trying to do is run a set of sql commands.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-09 : 10:02:34
and if your question was how to execute exe from sql you can use xp_cmdshell extended stored procedure for that

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

patrickjao
Starting Member

24 Posts

Posted - 2012-07-09 : 22:12:07
Thanks for the reply, I have gone through SQLCMD, it works, but not suitable for our usage, since all scripts is shown to the user, what we need is to create an exe to run a set of SQL commands/or SQL file(.sql)
awaiting your prompt reply
Thanks and regards

Patrick

quote:
Originally posted by sunitabeck

Did you mean how to create an exe to run a set of SQL commands?

If that is so: normally people do not create an .exe for sql commands. They use the SQLCMD utility. SQLCMD allows you to execute SQL statements you have stored in a text file, or even SQL commands provided as parameters to the utility. http://msdn.microsoft.com/en-us/library/ms162773.aspx

If you do want to distribute an exe, you would need to create a .net (or some such) application. But that is less desirable for a variety of reasons if all you are trying to do is run a set of sql commands.

Go to Top of Page

adbasanta
Posting Yak Master

120 Posts

Posted - 2012-07-09 : 23:11:32
quote:
Originally posted by patrickjao

Thanks for the reply, I have gone through SQLCMD, it works, but not suitable for our usage, since all scripts is shown to the user, what we need is to create an exe to run a set of SQL commands/or SQL file(.sql)
awaiting your prompt reply
Thanks and regards

Patrick

quote:
Originally posted by sunitabeck

Did you mean how to create an exe to run a set of SQL commands?

If that is so: normally people do not create an .exe for sql commands. They use the SQLCMD utility. SQLCMD allows you to execute SQL statements you have stored in a text file, or even SQL commands provided as parameters to the utility. http://msdn.microsoft.com/en-us/library/ms162773.aspx

If you do want to distribute an exe, you would need to create a .net (or some such) application. But that is less desirable for a variety of reasons if all you are trying to do is run a set of sql commands.





You can compress your sql procedures to a .dll format and use dotnet to link to that dll for execution.


-------------------------------------------------------------------------------------------------------
Learning MS SQL Server 2008
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-09 : 23:12:32
quote:
Originally posted by patrickjao

Thanks for the reply, I have gone through SQLCMD, it works, but not suitable for our usage, since all scripts is shown to the user, what we need is to create an exe to run a set of SQL commands/or SQL file(.sql)
awaiting your prompt reply
Thanks and regards

Patrick

quote:
Originally posted by sunitabeck

Did you mean how to create an exe to run a set of SQL commands?

If that is so: normally people do not create an .exe for sql commands. They use the SQLCMD utility. SQLCMD allows you to execute SQL statements you have stored in a text file, or even SQL commands provided as parameters to the utility. http://msdn.microsoft.com/en-us/library/ms162773.aspx

If you do want to distribute an exe, you would need to create a .net (or some such) application. But that is less desirable for a variety of reasons if all you are trying to do is run a set of sql commands.




why cant it be a scheduled job?has it to be based on user interaction? can you explain exact scenario?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

patrickjao
Starting Member

24 Posts

Posted - 2012-07-10 : 05:39:18
Thanks Visakh16, schedule job may be a good solution for me,

what I really want to do is to update a require data to a new table by using Drop table and create table commands in a query, if automatic schedule job can do ( may be automatic update every minute) , I t would be solve my problems.

please advice how?

Patrick



quote:
Originally posted by visakh16

quote:
Originally posted by patrickjao

Thanks for the reply, I have gone through SQLCMD, it works, but not suitable for our usage, since all scripts is shown to the user, what we need is to create an exe to run a set of SQL commands/or SQL file(.sql)
awaiting your prompt reply
Thanks and regards

Patrick

quote:
Originally posted by sunitabeck

Did you mean how to create an exe to run a set of SQL commands?

If that is so: normally people do not create an .exe for sql commands. They use the SQLCMD utility. SQLCMD allows you to execute SQL statements you have stored in a text file, or even SQL commands provided as parameters to the utility. http://msdn.microsoft.com/en-us/library/ms162773.aspx

If you do want to distribute an exe, you would need to create a .net (or some such) application. But that is less desirable for a variety of reasons if all you are trying to do is run a set of sql commands.




why cant it be a scheduled job?has it to be based on user interaction? can you explain exact scenario?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-10 : 06:45:50
Setting up a scheduled job is fairly straightforward - MSDN has step by step instructions here: http://msdn.microsoft.com/en-us/library/ms190268.aspx

Whether you use scheduler or use SQLCMD, one thing you might want to consider is creating a stored procedure with all the steps you need to do. You would then run the stored procedure from the scheduler or sqlcmd. It is more secure and safer, and also would ensure that the user would not see the steps that you are executing.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-10 : 12:37:24
why do you need to drop and create table to update some data? if you want to purge some earlier data you can use delete/trucate. no need to drop and re create tale each time for that.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

patrickjao
Starting Member

24 Posts

Posted - 2012-07-11 : 01:01:12
Thanks you very much ,visakh16, sunitabeck
well done, scheduler and Delete and Insert solve my problem, one more question, how to combine the record of 2 table into 1 table, for example : table 1 :
Name Status value 1
ALI D 10000
John D 15000
and Table 2 :
Name Status value 2
ALI K 5000
John K 10000
become Table 3:
Name Status value 1 Value 2
ALI D 10000
John D 15000
ALI K 5000
John K 10000



Go to Top of Page

patrickjao
Starting Member

24 Posts

Posted - 2012-07-11 : 01:09:46
Sorry! table 3 should be as follow

Name status Value1 Value2
Ali........D.....10000.....0
John.....D.....15000.....0
Ali........K........0.......5000
John.....K........0......10000
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-11 : 07:55:07
You can do a UNION of the two tables like this:
SELECT
NAME, STATUS, Value1, CAST(NULL AS INT) AS Value2
FROM
Table1
UNION ALL
SELECT
NAME, STATUS, NULL, Value2
FROM
Table2
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-11 : 10:36:19
and if they've duplicate and you want distinct set use UNION instead of UNION ALL

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

patrickjao
Starting Member

24 Posts

Posted - 2012-07-12 : 12:42:25
Thanks again, for small data table my problem solve. but still thinking how to handle if the table data is big and only need to update for the user who has no authority to use SQL management studio while nessasary, it is not wise to use scheduler automatic update all the time, it may loaded the server. Any good suggestion or solution?
Go to Top of Page

jleitao
Posting Yak Master

100 Posts

Posted - 2012-07-12 : 13:48:53
If you use TRUNCATE TABLE <table_name> instead DELETE it will be more faster. Probably more that UPDATE. Can you test it?
Go to Top of Page
   

- Advertisement -