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.aspxIf 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. |
 |
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
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 replyThanks and regardsPatrickquote: 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.aspxIf 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.
|
 |
|
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 replyThanks and regardsPatrickquote: 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.aspxIf 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 |
 |
|
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 replyThanks and regardsPatrickquote: 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.aspxIf 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
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?Patrickquote: 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 replyThanks and regardsPatrickquote: 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.aspxIf 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 MVPhttp://visakhm.blogspot.com/
|
 |
|
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.aspxWhether 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. |
 |
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
patrickjao
Starting Member
24 Posts |
Posted - 2012-07-11 : 01:01:12
|
Thanks you very much ,visakh16, sunitabeckwell 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 1ALI D 10000John D 15000and Table 2 :Name Status value 2ALI K 5000John K 10000become Table 3:Name Status value 1 Value 2ALI D 10000 John D 15000 ALI K 5000John K 10000 |
 |
|
patrickjao
Starting Member
24 Posts |
Posted - 2012-07-11 : 01:09:46
|
Sorry! table 3 should be as followName status Value1 Value2Ali........D.....10000.....0John.....D.....15000.....0Ali........K........0.......5000John.....K........0......10000 |
 |
|
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 Value2FROM Table1UNION ALLSELECT NAME, STATUS, NULL, Value2FROM Table2 |
 |
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
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? |
 |
|
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? |
 |
|
|