This question already has answers here:
A table name as a variable
(10 answers)
Closed 9 years ago.
I have try to use statement below in my SQL. Is it possible by any means to dynamically select table form database. (For example select table name from Comobox and then display data from selected table)
SET #var1 = 'test';
SELECT * From #var1
Dynamic SQL requires that you create a string consisting of the sql statement, and then you execute the string. Dynamic t-sql quotes in string
Related
This question already has answers here:
Possible to store value of one select column and use it for the next one?
(4 answers)
How to use a calculated column to calculate another column in the same query using a subquery
(2 answers)
SQL Server Reference a Calculated Column
(6 answers)
Using CASE on a Calculated Column in SQL Server 2012
(2 answers)
Closed 6 months ago.
I am trying to calculate the date difference and keep getting a column name error. I am new to SQL and learning from books and YouTube. Any assistance would be appreciated. I commented out the code not working
declare #rpDT datetime
set #rpDT = getdate()
SELECT [OrgKey]
,[visID]
,[visPatID]
,[visInternal]
,[visName]
,[visAssignedNS]
,[visAssignedRoom]
,[visAssignedBed]
,[visAdmitDT]
,isnull([visDischargeDT],#rpDT)as disDT
,datediff(day,[visAdmitDT],disDT) as Pt_days
FROM [MH_Pharmacy_Hub].[MC].[dbo_Visits]
SQL Server and most of the RDBMS do not allow to reference column at the same level. The closest equivalent to achieve similar effect is CROSS APPLY:
declare #rpDT datetime = getdate();
SELECT [OrgKey]
,[visID]
,[visPatID]
,[visInternal]
,[visName]
,[visAssignedNS]
,[visAssignedRoom]
,[visAssignedBed]
,[visAdmitDT]
,s.disDT
,datediff(day,[visAdmitDT],s.disDT) as Pt_days
FROM [MH_Pharmacy_Hub].[MC].[dbo_Visits]
CROSS APPLY (SELECT COALESCE([visDischargeDT],#rpDT)) AS s(disDT);
This question already has answers here:
A SQL Query to select a string between two known strings
(18 answers)
Closed 2 years ago.
I have a string like
https:\/\/www.abc.ca\/v3\/homes\/190135717\/responses\/bulk?page=5&per_page=100
I want everything after the equal sign = and before &, i.e the number 5 in SQL Server
Try this below logic using SUBSTRING and CHARINDEX:
Demo Here
DECLARE #T VARCHAR(200) = 'https:\/\/www.abc.ca\/v3\/homes\/190135717\/responses\/bulk?page=5&per_page=100'
SELECT SUBSTRING(#T,CHARINDEX('=',#T,0)+1,CHARINDEX('&',#T,0)-CHARINDEX('=',#T,0)-1)
This question already has answers here:
I need to pass column names using variable in select statement in Store Procedure but i cannot use dynamic query
(3 answers)
Where can parameters be used in SQL statements?
(2 answers)
Using SQL parameters to DROP INDEX in C#
(2 answers)
Closed 5 years ago.
How can I pass variable in Group BY expression.Here is my code.
and from #Varientsku I want to pass column name
Create Proc spIsUnique
#varientsku nvarchar(max) output
As
Begin
select #varientsku,
IIf (count(*)>1,'Dublicate','True') as Total from product group by
#varientsku
End.
and I get this error.
Each GROUP BY expression must contain at least one column that is not an outer reference.
Help me to solve this
You need dynamic sql
EXEC ('select '+#varientsku+',
IIf (count(*)>1,''Duplicate'',''True'') as Total
from product
group by '+#varientsku)
Note : Make sure #varientsku is sanitized, this code is vulnerable to sql injection
This question already has answers here:
Upserting in MS-access
(6 answers)
Closed 7 years ago.
I want to check whether a row exists or not before inserting a new one in Access 2007.
I have the following working query in SQL Server but I'm having trouble converting it to Access.
update category set name='hell' where categoryid=287
if ##rowcount=0
begin
insert into category(categoryid,name,path,parentcategoryid,creationdate) values (287,'a','a',12,'')
end
Try this
update category set name='hell' where categoryid=287;
if not exists(select * from Category where categoryid=287)
insert into category(categoryid,name,path,parentcategoryid,creationdate)
values (287,'a','a',12,'');
This question already has answers here:
How do I ignore ampersands in a SQL script running from SQL Plus?
(7 answers)
Closed 7 years ago.
One of my oracle database columns needs to have the value 'user.password&user.user_name'
When I execute the query, I see a popup, because the query has '&user' in it. How do I get around it?
Example query:
update Table A set value = 'user.password&user.user_name' where id = 1;
Modify the query to insert the value
'user.password'||chr(38)||'user.user_name'
Example query:
update Table A set value = 'user.password'||chr(38)||'user.user_name' where id = 1;
You can find several decisions at How do I ignore ampersands in a SQL script running from SQL Plus?
Btw, you can use just update table set value = &value where id = 1 and type your value into popup.
chr(38) should work independent of the platform.
However, if you are using SQL*Plus, then you could use the sqlplus command:
set define off
In SQL*Plus, ampersand is used for substitution variable. The above command will ignore it.
For example,
SQL> SET DEFINE OFF
SQL> SELECT 'user.password&user.user_name' FROM DUAL;
'USER.PASSWORD&USER.USER_NAM
----------------------------
user.password&user.user_name
SQL>
Well, of course,chr(38) will work too:
SQL> SELECT 'user.password'||chr(38)||'user.user_name' FROM dual;
'USER.PASSWORD'||CHR(38)||'U
----------------------------
user.password&user.user_name
SQL>