SQL Server stored procedure for query generation based on table name (query criteria optional)

Source: Internet
Author: User

  static void Main (string[] args) {string table name = "Water_emstime";            String sql = "Exec gettableselect" + table name;            String constring = "Server=xxx.xxx.xx.xx;database=newfw;uid=sa;pwd=sa";            SqlDataAdapter da = new SqlDataAdapter (SQL, constring);            DataSet ds = new DataSet (); Da.            Fill (DS);            String parmslist = ""; foreach (DataRow item in DS. Tables[0]. Rows) {parmslist + = item[0].            ToString () + "\ n";            } String select = ""; Select + = ds. TABLES[1]. Rows[0][0].            ToString () + "\ n"; foreach (DataRow item in DS. TABLES[2]. Rows) {Select + = item[0].            ToString () + "\ n"; }//query stored procedure generated from table string procselect = string. Format (@ "CREATE proc [dbo].[                                Get_{0}] ({1})         As                       Begin {2} end ", table name, Parmslist, select );//table name, parameter list, query statement//CREATE PROC [dbo].                    [Oper_water_alarm]//(//@Param_monitoring_mn varchar (+) =null,    @Param_factor_id varchar (=null),//@Param_datatime datetime=null,// @Param_value Numeric (9) =null,//@Param_flag varchar (=null),//@Param_ac tion int//)//as//begin//if (@Param_acti                            on=0)//Begin//INSERT INTO Water_alarm (MONITORING_MN,                            factor_id,//datatime,//value,      Flag)//VALUES (@Param_monitoring_mn,                      @Param_factor_id,//@Param_datatime,//@Para                    M_value,//@Param_flag); End/Else if (@Param_action =1)//Begin/DE                    Lete Water_alarm//WHERE [EMAIL PROTECTED]_MONITORING_MN//End else/Begin//Update water_alarm Set [email p ROTECTED]_MONITORING_MN,//[email protected]_factor_id,//[EMA Il protected]_datatime,//[Email protected]_value,//[EM    Ail protected]_flag//WHERE [EMAIL PROTECTED]_MONITORING_MN// End//end}

SQL Server stored procedure for query generation based on table name (query criteria optional)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.