TSQL IF / ELSE或CASE(UPSERT)

时间:2017-12-07 16:57:52

标签: sql-server tsql

不确定IF / ELSE是否是正确的方法。它总是返回ELSE,因此它似乎无法正常工作。

IF ((SELECT COUNT(CAST(StudentuserID AS int)) FROM StudentAttendance WHERE StudentUserID=1)>0)
PRINT 'Yes'
ELSE 
PRINT 'No'

此测试应生成yes,因为数据为8>0
我将使用PRINT ELSE UPDATE语句替换INSERT

IF ((SELECT COUNT(CAST(StudentuserID AS int)) FROM StudentAttendance WHERE StudentUserID=1)>0)
UPDATE StudentAttendance
SET
CID = CAST('[querystring:CID]' AS int),
CalendarEventID = CAST('[querystring:CEID]' AS int),
StudentUserID = CAST('[StudentUserID]' AS int),
Attendance = '[Attendance]'
ELSE
INSERT INTO StudentAttendance
(CID,CalendarEventID,StudentUserID,Attendance)
VALUES
(CAST('[querystring:CID]' AS int), CAST('[querystring:CEID]' AS int), CAST('[StudentsUserID]' AS int),'[Attendance]')

1 个答案:

答案 0 :(得分:1)

看起来您的import tensorflow as tf import process_images as image_util from tensorflow.contrib.data import Dataset, Iterator # With MNIST #from tensorflow.examples.tutorials.mnist import input_data #mnist = input_data.read_data_sets("MNISt_data/", one_hot=True) filenames_dummy, labels_dummy = image_util.run_it() #The filenames_dummy and labels_dummy are two lists looking like this, respectively: #["data/image_1.png", "data/image_2.png", ..., "data/image_n.png"] # The values of the labels are 0-3, since I have 4 classes. #[0, 1, ..., 3] filenames = tf.constant(filenames_dummy) labels = tf.constant(labels_dummy) def _parse_function(filename, label): image_string = tf.read_file(filename) image_decoded = tf.image.decode_png(image_string, channels=3) # The image size is 425x425. image_resized = tf.image.resize_images(image_decoded, [425,425]) return image_resized, label dataset = tf.contrib.data.Dataset.from_tensor_slices((filenames, labels)) dataset = dataset.map(_parse_function) dataset = dataset.batch(30) dataset = dataset.repeat() iterator = dataset.make_one_shot_iterator() # Helper functions # INIT weights def init_weights(shape): init_random_dist = tf.truncated_normal(shape, stddev=0.1) return(tf.Variable(init_random_dist)) # INIT Bias def init_bias(shape): init_bias_vals = tf.constant(0.1, shape=shape) return tf.Variable(init_bias_vals) # CONV2D def conv2d(x, W): # x --> input tensor [batch, H, W, Channels] # W --> [filter H, filter W, Channels IN, Channels OUT] return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME') # Pooling def max_pooling_2by2(x): # x --> [batch, h, w, c] return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME') #Convolutional layer def convolutional_layer(input_x, shape): W =init_weights(shape) b = init_bias([shape[3]]) return tf.nn.relu(conv2d(input_x, W)+b) # Normal (FULLY CONNTCTED) def normal_full_layer(input_layer, size): input_size = int(input_layer.get_shape()[1]) W = init_weights([input_size, size]) b = init_bias([size]) return tf.matmul(input_layer, W) + b # PLACEHOLDERS x = tf.placeholder(tf.float32, shape=[None, 180625]) y_true = tf.placeholder(tf.float32, shape=[None, 4]) # With MNIST #x = tf.placeholder(tf.float32, shape=[None, 784]) #y_true = tf.placeholder(tf.float32, shape=[None, 10]) # Layers x_image = tf.reshape(x, [-1, 425,425, 1]) # With MNIST #x_image = tf.reshape(x, [-1, 28,28, 1]) convo_1 = convolutional_layer(x_image, shape=[5,5,1,32]) convo_1_pooling = max_pooling_2by2(convo_1) convo_2 = convolutional_layer(convo_1_pooling, shape=[5,5,32, 64]) convo_2_pooling = max_pooling_2by2(convo_2) convo_2_flat = tf.reshape(convo_2_pooling, [-1, 7*7*64]) full_layer_one = tf.nn.relu(normal_full_layer(convo_2_flat, 1024)) # Dropout hold_prob = tf.placeholder(tf.float32) full_one_dropout = tf.nn.dropout(full_layer_one, keep_prob=hold_prob) y_pred = normal_full_layer(full_one_dropout, 4) # With MNIST #y_pred = normal_full_layer(full_one_dropout, 10) # LOSS function cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_pred)) # Optimizer optimizer = tf.train.AdamOptimizer(learning_rate=0.001) train = optimizer.minimize(cross_entropy) init = tf.global_variables_initializer() steps = 5000 with tf.Session() as sess: sess.run(init) for i in range(steps): batch_x, batch_y = iterator.get_next() test1, test2 = sess.run([batch_x, batch_y]) # With MNIST #sess.run(train, feed_dict={x:batch_x, y_true:batch_y, hold_prob:0.5}) if i%100 == 0: print("ON STEP {}".format(i)) print("Accuracy: ") matches = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y_true, 1)) accuracy = tf.reduce_mean(tf.cast(matches, tf.float32)) # With MNIST #print(sess.run(accuracy, feed_dict={x:mnist.test.images, y_true:mnist.test.labels, hold_prob:1.0})) / IF可以正常工作(看起来您正在为存储过程中的一条记录执行此操作吗?)。如果它正在返回'否'而且你不应该认为它应该是,我可能会对你的桌子做一个更基本的检查,例如:

ELSE

您也可以使用SELECT * FROM StudentAttendance WHERE StudentUserID = 1 语句,并且可以通过在MERGE部分中加入多个源表来使用它们。这是一个基本的例子:

USING