如何在SQL Server中添加动态列,这是从声明变量中选择列名称

时间:2018-07-02 06:48:20

标签: sql sql-server

<html>
<head>
<title>Demo</title>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="http://www.appelsiini.net/download/jquery.jeditable.mini.js"></script>
<script>
//<![CDATA[
jQuery(function ($) {
    $("#newSubStatus_A").change(function () {
        $("#showhide_A").toggle(this.value == 'Option B');
    }).change(); //to set the initial state
})

jQuery(function ($) {
    $('#subStatus').on('change', 'select', function (event) {
      $('#showhide_B').toggle(this.value == 'Option B');
    })
})

jQuery(function ($) {
    $('#subStatus').on('click', 'button', function (event) {
        $('#showhide_B').hide();
    })
})
function jeditableReset(settings, original) {
    $('#showhide_B').hide();
}
  
$(function() {
	$(".edselect_appStatus").editable("./ADMIN.UPDATER.php?key=demo", {
		data		: '{"Stat A":"Stat A","Stat B":"Stat B","Stat C":"Stat C","Stat D":"Stat D","Stat E":"Stat E"}',
		callback	: function() {
			location.reload();
		},
		type			: 'select',
		event		: 'dblclick',
		placeholder	: '',
		onblur		: 'ignore',
		indicator	: 'Saving...',
		cssclass	: 'editable',
		submit		: 'Save',
		cancel		: 'Cancel',
	});

	$(".edselect_subStatus").editable("./ADMIN.UPDATER.php?key=demo", {
		data         : '{"Option A":"Option A","Option B":"Option B","Option C":"Option C","Option D":"Option D","Option E":"Option E","REMOVE":"Remove Sub-status"}',
		callback	: function() {
			location.reload();
		},
    onreset: jeditableReset,
    type			: 'select',
		event		: 'dblclick',
		placeholder	: '',
		onblur		: 'ignore',
		indicator	: 'Saving...',
		cssclass	: 'editable',
		submit		: 'Save',
		cancel		: 'Cancel',
	});
});
//]]>
</script>
</head>

<body style="bgcolor: #fff;">

<h1>Original method</h1>

<p>To see effect, choose &quot;Option B&quot; in Sub-Status</p>

    <table style="width: 800px; margin-bottom: 0;" border="1">
     <tbody><tr>
      <td style="width: 17%;">Status:</td>
      <td style="bgcolor: #f6f6f6; width: 33%;">
	   <select id="newStatus_A" name="newStatus_A" style="width: 270px;">
       <option value="">Choose...</option>
       <option value="Stat A">Stat A</option>
       <option value="Stat B">Stat B</option>
       <option value="Stat C">Stat C</option>
       <option value="Stat D">Stat D</option>
       <option value="Stat E">Stat E</option>
      </select></td>
      <td style="width: 17%; padding-left: 10px;">Sub-status:</td>
      <td style="bgcolor: #f6f6f6; width: 33%;">
	   <select id="newSubStatus_A" name="newSubStatus_A" style="width: 270px;">
       <option value="">Choose...</option>
       <option value="Option A">Option A</option>
       <option value="Option B">Option B</option>
       <option value="Option C">Option C</option>
       <option value="Option D">Option D</option>
       <option value="Option E">Option E</option>
       <option value="REMOVE">Remove Sub-status</option>
      </select><br>
      <div id="showhide_A" style="display: none;">
       <input type="text" style="width: 120px; margin-left: 40px;" name="newSubStatusExtra_A" value="" placeholder="Enter amount">
      </div></td>
     </tr></tbody>
    </table>

<br><br>

<h1>New method (using Jeditable)</h1>

<p>Double click in on the &quot;Select...&quot; fields. Choosing &quot;Option B&quot; in Sub-Status field is supposed to reveal the additional field as above</p>

    <table style="width: 800px; margin-bottom: 0;" border="1">
     <tbody><tr>
      <td style="width: 17%;">Status:</td>
      <td style="bgcolor: #f6f6f6; width: 33%;" class="edselect_appStatus" id="appStatus">Select...</td>
      <td style="width: 17%; padding-left: 10px;">Sub-status:</td>
      <td style="bgcolor: #f6f6f6; width: 33%;" id="subStatus"><div class="edselect_subStatus">Select...</div>
        
      <div id="showhide_B" style="display: none;"><br>
       <input type="text" style="width: 120px; margin-left: 40px;" name="newSubStatusValue" maxlength="12" value="" placeholder="Enter amount">
      </div></td>
     </tr></tbody>
    </table>

</body>
</html>

更改表dbo。客户添加@Columnnew

我需要那样。

3 个答案:

答案 0 :(得分:0)

您可以使用EXEC执行此动态sql,例如:

DECLARE @sqlCommand varchar(1000)    
DECLARE @Columnnew varchar(50)

SET @Columnnew ='NewColumn12'

SET @sqlCommand = 'ALTER TABLE dbo.Customers ADD ' + @Columnnew + ' varchar(MAX)'
EXEC (@sqlCommand)

有关更多信息,请参见here

答案 1 :(得分:0)

您可以使用动态sql外观:

declare @Columnnew varchar(50)
declare @SQL varchar(300)

set @Columnnew ='NewColumn12'
set @SQL = 'alter table dbo.Customers add ' || @Columnnew ||'
execute sql

答案 2 :(得分:0)

通过红色查询尝试此操作,如下所示:

declare @Columnnew varchar(50),@Query nvarchar(max)=''
set @Columnnew ='NewColumn12'
set @Query ='alter table dbo.Customers add ' +@Columnnew
exec (@Query )