我的软件只能从表格或视图中读取。我需要从select * from my_view WHERE somefield = 'data_identificator'
方法向该软件提供一些数据(例如来自Web服务的数据)。该软件将读取如下数据:
clr
然后{{1}}需要将somefield作为参数并执行其他一些代码,然后返回类似于view的结果。它有可能吗?
答案 0 :(得分:1)
如果提供了预期代码的完整示例而不仅仅是其中的一部分,那么这个问题会更容易回答,但我认为我理解的足以指导正确的方向。
是的,可以使用SQLCLR从外部源(如Web服务等)获取数据。您可以编写专门的标量或表值函数来调用特定方法并返回已分析的输出。或者,您可以创建一个返回结果XML的泛型函数,然后在T-SQL中解析它。
如果需要执行多个步骤,则可以从T-SQL多语句表值函数调用该SQLCLR函数。这甚至可以让你传递参数。
您的软件"只能从表格或视图中阅读" 应该能够从这个多语句表值函数(TVF)中SELECT
,因为它就像一个可以传递参数的视图。如果您的软件因任何原因无法从TVF中选择,那么您可以将SELECT field1, field2, ... FROM dbo.MyTVF();
打包在View
。
您如何编写这样的SQLCLR函数来调用Web服务?没那么快。如果您首先提出这个问题,那么将这种性质的代码复制并粘贴到项目中可能弊大于利。是的,在SQLCLR函数或过程中调用Web服务的各个站点上有几个例子,甚至可能在这里,但有些(甚至可能是大多数?)都做得很差。即使您在.NET编程方面经验丰富,但您需要了解SQL Server的CLR主机还有一些细微差别。因此,如果不首先了解环境的约束以及如何正确地与SQL Server交互,您真的不应该编写SQLCLR代码。为了解决这个问题,我开始编写一个关于SQL Server Central的系列文章:Stairway to SQLCLR(需要免费注册)。
我还要提到,对于任何有兴趣调用URI但不愿意或无法编写任何代码的人来说,{{3}中有一个名为 INET_GetWebPages 的表值函数执行此操作的SQLCLR库。完全披露:我是SQL#的作者,虽然有免费版本,但 INET_GetWebPages 功能仅在完整版中可用。
答案 1 :(得分:0)
所以,经过一些研究并根据srutzky和JamesZ的最终答案的评论。
function getChart(id) {
var allCharts = AmCharts.charts;
for (var i = 0; i < allCharts.length; i++) {
if (id == allCharts[i].div.id) {
return allCharts[i];
}
}
}
function load_holding_graph(graph_type, history, title, aggregate, industry){
var title_1 = industry[0].ticker + '\n';
var title_2 = industry[0].name;
var title_3 = industry[0].industry + ' . ' + industry[0].sector;
// current day exclude weekends
var current_date = new Date();
switch(current_date.getDay()){
case 0:
var s = new Date(current_date.getTime());
var d = s.setDate(current_date.getDate() - 1);
break;
case 6:
var s = new Date(current_date.getTime());
var d = s.setDate(current_date.getDate() - 2);
break;
default:
var d = current_date;
}
var latest_trading_day = $.datepicker.formatDate('dd MM, yy', d);
// CREATE DATA
var chartData = [];
generateChartData(history);
function generateChartData() {
for (var i = 0; i < history.length; i++) {
var date = new Date(history[i].date);
var val = Math.round(Math.random() * (30) + 100);
chartData[i] = ({
date: date,
open: history[i].open,
close: history[i].close,
high: history[i].high,
low: history[i].low,
volume: history[i].volume,
value: val
});
}
}
// CHART CONFIG
var chartConfig = {
type: "stock",
pathToImages : "/static/img/amcharts/",
addClassNames:true,
dataSets: [{
fieldMappings: [{
fromField: "open",
toField: "open"
}, {
fromField: "close",
toField: "close"
}, {
fromField: "high",
toField: "high"
}, {
fromField: "low",
toField: "low"
}, {
fromField: "volume",
toField: "volume"
}, {
fromField: "val",
toField: "val"
}
],
color: "#fff",
dataProvider: chartData,
title: title,
categoryField: "date",
compared: false,
},
{
fieldMappings: [{
fromField: "value",
toField: "value"
}],
color: "#fff",
dataProvider: chartData,
title: title,
categoryField: "date"
},
],
panels: [{
addClassNames:true,
percentHeight: 75,
valueAxes: [{
id:"v1",
//logarithmic:true,
unit : '$',
unitPosition:"left",
position:"left",
},
{
id:"v2",
unit : '$',
unitPosition:"left",
position:"right",
synchronizeWith: "v1",
synchronizationMultiplier: 1,
},
],
marginBottom: 0,
marginTop: 0,
stockGraphs: [{type: graph_type,
id: "g1",
title:title,
openField: "open",
closeField: "close",
highField: "high",
lowField: "low",
valueField: "close",
lineColor: "#11EDF1",
fillColors: "#11EDF1",
negativeLineColor: "#db4c3c",
negativeFillColors: "#db4c3c",
fillAlphas: 1,
// comparedGraphLineThickness: 2,
columnWidth: 0.4,
useDataSetColors: false,
comparable: true,
compareField: "close",
bullet : "round",
bulletAlpha : 0,
showBalloon: true,
showBalloonAt:"top",
"balloonText":"$[[value]]",
} ],
stockLegend: {
enabled:true,
useGraphSettings:true,
data:[{title: ""}],
// valueTextRegular: undefined,
periodValueTextComparing: "[[percents.value.close]]%",
markerType: "diamond",
backgroundColor :"#ffffff",
markerSize : 24,
position:"top",
},
},
],
panelsSettings: {
color: "#fff",
plotAreaFillColors: "#333",
plotAreaFillAlphas: 1,
marginLeft: 60,
marginRight: 60,
marginTop: 20,
marginBottom: 5,
},
categoryAxesSettings: {
equalSpacing: true,
gridColor: "#555",
gridAlpha: 1,
maxSeries:0,
markPeriodChange:false,
autoGridCount:true,
minHorizontalGap:100,
dateFormats: [{period:'fff',format:'JJ:NN:SS'},
{period:'ss',format:'JJ:NN:SS'},
{period:'mm',format:'JJ:NN'},
{period:'hh',format:'JJ:NN'},
{period:'DD',format:'MMM DD, YYYY'},
{period:'WW',format:'MMM DD'},
{period:'MM',format:'MMM YYYY'},
{period:'YYYY',format:'YYYY'}],
},
valueAxesSettings: {
gridColor: "#555",
gridAlpha: 1,
inside: false,
showLastLabel: true,
},
chartCursorSettings: {
pan: true,
valueLineEnabled:true,
valueLineBalloonEnabled:true,
onePanelOnly:true,
},
legendSettings: {
color: "#fff"
},
stockEventsSettings: {
showAt: "high"
},
balloon: {
textAlign: "left",
offsetY: 10,
},
"export": {
"enabled": true,
"backgroundColor": "#fff",
},
}
AmCharts.charts = [];
// CREATE CHART
var chart = AmCharts.makeChart("holding-graph",chartConfig);
console.log(chart)
}
如果我CREATE VIEW [dbo].[MyView]
AS
SELECT a.*, s.ValueFromTvf
FROM HelpTable a
CROSS APPLY dbo.MyClrFunction(a.PropertyA) s
从该观点出发:
SELECT * FROM MyView
在哪里PropertyA ='123456'
MyClrFunction将使用参数'123456'执行。