tsql

INSERT INTO multiple rows with subquery

守給你的承諾、 提交于 2020-01-25 11:39:06
问题 I want to INSERT INTO by more than one value at a time, I did this by doing the following: INSERT INTO table_1(col_1,col_2,col_3) SELECT col_1, col2, (SELECT col3 FROM table_3) FROM table_2 But col3 from table_3 is a datetime format, while col3 from table_1 needs an integer value. I did this by doing the following: CAST(CONVERT(varchar(10),(SELECT col3 FROM table_3),112)AS int) When I run this I get a more than one result in a subquery error. I have really no idea whatsoever on how to fix

The correct syntax for a T-SQL subquery and a possible join

北城以北 提交于 2020-01-25 11:38:09
问题 What would be the correct syntax and join (if any) of a subquery that would return all of the employees first and last name from the employee’s table, and return their department name from the department table, but only those employees who more than the average salary for their department? Thanks for your answers 回答1: This query should give you what you are looking for. select firstName, lastName, departmentName from Employees e join (select departmentID, departmentName, AVG(salary) AS

creating / updating view using stored procedure

若如初见. 提交于 2020-01-25 11:00:48
问题 I want to create or update a view using stored procedure like this: CREATE PROC Proc_Get_Ready_Weapons AS BEGIN IF EXISTS(select * FROM sys.views where name = 'dbo.vwGetReadyWeapons') BEGIN EXEC ('CREATE VIEW dbo.vwGetReadyWeapons ... rest of view') END ELSE BEGIN EXEC ('CREATE OR REPLACE VIEW dbo.vwGetReadyWeapons ... rest of view') END IF @@ROWCOUNT = 0 PRINT 'Warning: No rows were updated' END But getting this error: Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'OR'

Get top 1 row of each group

可紊 提交于 2020-01-25 09:26:28
问题 I have a table which I want to get the latest entry for each group. Here's the table: DocumentStatusLogs Table |ID| DocumentID | Status | DateCreated | | 2| 1 | S1 | 7/29/2011 | | 3| 1 | S2 | 7/30/2011 | | 6| 1 | S1 | 8/02/2011 | | 1| 2 | S1 | 7/28/2011 | | 4| 2 | S2 | 7/30/2011 | | 5| 2 | S3 | 8/01/2011 | | 6| 3 | S1 | 8/02/2011 | The table will be grouped by DocumentID and sorted by DateCreated in descending order. For each DocumentID , I want to get the latest status. My preferred output:

patindex t-sql special characters

梦想的初衷 提交于 2020-01-25 08:33:05
问题 How to check if any of this !@#$%^&*()_-+ special characters exist in a string ? i tried SELECT PATINDEX('!@#$%^&*()_-+', 'test-'); SELECT PATINDEX('[!@#$%^&*()_-+]', 'test-'); SELECT PATINDEX('%[!@#$%^&*()_-+]%', 'test-'); but all returns 0, it should return 5, any help ? 回答1: The - is a special character in the LIKE or PATINDEX() pattern. If it is anywhere other than the first position, it is a range of characters -- such as all digits being represented by [0-9] . You can do what you want

Deadlock when querying INFORMATION_SCHEMA

為{幸葍}努か 提交于 2020-01-25 08:26:28
问题 I have a process which dynamically alters my SQL2K5 table structure according to changes in a published meta-data layer. For example, if a new column needs to be added and the table has NO dependancies - the steps would be: 1. Create scripts using T-SQL for any indexes & primary keys that already exist on the table [these scripts are included below] 2. Drop the table 3. Re-create the table from the meta-layer that has the new column 4. Execute the scripts created in step#1 5. Populate the

Unable to create a stored procedure in SSMS

感情迁移 提交于 2020-01-25 07:31:16
问题 I am trying to create a stored procedure in SSMS to export the query result to CSV. But I am getting below error while creating. SQL statement: CREATE PROCEDURE SelectUsers AS SELECT * FROM [IMBookingApp].[dbo].[usertest] INTO OUTFILE 'C:/S3/users.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'; GO; Error Msg 156, Level 15, State 1, Procedure SelectUsers, Line 4 [Batch Start Line 0] Incorrect syntax near the keyword 'INTO' Any help would be appreciated. 回答1: try the

Split column based on ascii value

妖精的绣舞 提交于 2020-01-25 07:05:24
问题 I have a text field in a table that contains a large string, each part of the string that i want to separate is split by a little square. When searching I found out this could be an ascii value, so i run this ascii(substring(image_data, 18,1)) which returned 27 How would go about splitting this field into separate fields based on this ascii value? Thanks in advance, Chris EDIT: Example of what the data currently looks like. Having the TEXT before the = as the Header would be great if it is

Incrementing RECORD_ID When insterting records using a trigger

戏子无情 提交于 2020-01-25 07:01:06
问题 I am using a trigger to insert rows into a table using INSERT statement as below but when doing this the RECORD_ID number increments by 1 digit so all the records inserted have the same number.. This is what i'm using to increment the records from the trigger. , ISNULL(( SELECT MAX([PROGRESS-RECID]) FROM [DBAdmin].[dbo].[ReTncyTransStatement] ),0) + 1 AS [PROGRESS-RECID] This is what i'm using to load the data ;WITH TestTrans ( [ORG-CODE] ,[TNCY-SYS-REF] ,[TRANS-NO] ,[POSTING-YEAR] ,[POSTING

Incrementing RECORD_ID When insterting records using a trigger

♀尐吖头ヾ 提交于 2020-01-25 07:00:39
问题 I am using a trigger to insert rows into a table using INSERT statement as below but when doing this the RECORD_ID number increments by 1 digit so all the records inserted have the same number.. This is what i'm using to increment the records from the trigger. , ISNULL(( SELECT MAX([PROGRESS-RECID]) FROM [DBAdmin].[dbo].[ReTncyTransStatement] ),0) + 1 AS [PROGRESS-RECID] This is what i'm using to load the data ;WITH TestTrans ( [ORG-CODE] ,[TNCY-SYS-REF] ,[TRANS-NO] ,[POSTING-YEAR] ,[POSTING