dynamic-sql

How to Build select Query split Temp Value to two column one Per Number And Another to Text when Flag Allow 1?

佐手、 提交于 2020-08-10 23:24:50
问题 I work on a query for SQL Server 2012. I have an issue: I can't build select Query split Column Temp value to two Column When row in the temp table #nonparametric has the flag Allow = 1, it must split column Temp value from #nonparametric to two column when the flag Allow = 1 . suppose column Temp value has value 50.40 kg it must split to two column First column with number so it will have 50.40 and it will be same Name as Parametric . Second column with Text so it will have kg and it will be

Debugging long dynamic sql in SQL Server 2008

和自甴很熟 提交于 2020-07-21 06:37:50
问题 I have some dynamic sql statement which bombs under certain conditions, so I am trying to debug it. it gets built like so: declare @sql varchar(4000); ... select @sql = '<part1>'; ... select @sql = @sql + '<part2>'; ... select @sql = @sql + '<part3>'; ... begin execute(@sql); select @ec__errno = @@error if @ec__errno != 0 begin if @@trancount != 0 begin rollback; end return @ec__errno; end; ... As I said, it bombs in a particular iteration of a loop (don't ask me why it is implemented like

Store the value of a query into a SQL Server variable

南楼画角 提交于 2020-06-17 09:44:57
问题 The objective of the code is to run a query dynamically and return 0 if there are no rows with data present in the columns and to return 1 if there are rows with data in the columns. This is my code for the stored procedure: ALTER proc [dbo].[usp_ColumnFieldValidator] ( @TblName nvarchar(30), @ColumnName nvarchar(30), @RetVal bit output ) as begin declare @CountOfRowsQuery as nvarchar(300) set @CountOfRowsQuery = 'select count('+quotename(@ColumnName)+') from '+quotename(@TblName)+' having

Store the value of a query into a SQL Server variable

纵然是瞬间 提交于 2020-06-17 09:44:49
问题 The objective of the code is to run a query dynamically and return 0 if there are no rows with data present in the columns and to return 1 if there are rows with data in the columns. This is my code for the stored procedure: ALTER proc [dbo].[usp_ColumnFieldValidator] ( @TblName nvarchar(30), @ColumnName nvarchar(30), @RetVal bit output ) as begin declare @CountOfRowsQuery as nvarchar(300) set @CountOfRowsQuery = 'select count('+quotename(@ColumnName)+') from '+quotename(@TblName)+' having

Issues encountered with dynamic SQL

爱⌒轻易说出口 提交于 2020-06-01 07:36:06
问题 ALTER PROCEDURE [dbo].[Create_Subjects] @Subj_ID nvarchar(9) AS DECLARE @First3Digits nvarchar(3); DECLARE @Result int; DECLARE @Sql nvarchar(max) -- Fetching the fiest 3 digits of the subject SET @First3Digits = SUBSTRING(@Subj_ID,1,3); -- Check if view is present or not IF EXISTS (SELECT 1 FROM sys.views WHERE Name = @First3Digits) BEGIN SET @Sql = 'select @Result = case when exists (select 1 from dbo.' + quotename(@First3Digits) + ' where SubjectName = ''' + @Subj_ID + ''') then 1 else 0

pass my table name as parameter in sql query

痴心易碎 提交于 2020-04-30 07:44:25
问题 I need help with a query. In my query I want to pass my table name as parameter. This is my query: SELECT DISTINCT CONVERT (varchar, InspectDateTime) AS 'Inspect Date Time', CONVERT (varchar, SynDateTime) AS 'Sync Date Time', Employee, ROUND(OverAllPercentage, 2) AS Grade FROM Table_Name WHERE (DATEADD(dd, DATEDIFF(dd, 0, InspectDateTime), 0) BETWEEN DATEADD(dd, DATEDIFF(dd, 0, @From ), 0) AND DATEADD(dd, DATEDIFF(dd, 0, @To ), 0)) ORDER BY 'Inspect Date Time' Here I want to pass the Table